Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Harris__John
Beginner
187 Views

"if constexpr" and "missing return statement..." in icpc v 19

in icpc (ICC) 19.0.0.117 20180804, if I have a function with auto return type and a non-ambiguous "if constexpr" stanza like this:

template<typename ... ARGS> auto foo(ARGS ... args)
{
  if constexpr (sizeof...(args) == 1)
  {
    return /* variable of some type */;
  }
  else
  {
    return /* variable of another type */
  }
}


I get error  #1011: missing return statement at end of non-void function.  GCC is fine with it.  I can't figure a work-around except to fall back to SFINAE and enable_if

0 Kudos
7 Replies
Viet_H_Intel
Moderator
187 Views

you can add -wd1011 to your command line. 

Thanks,

Viet

Harris__John
Beginner
187 Views

That's great, and works.  But is there a way to apply that in a limited scope?  I tried

#pragma warning (disable:1011)

...but that didn't work.

Viet_H_Intel
Moderator
187 Views

$ icpc t.cpp -c -w3
t.cpp(1): remark #1418: external function definition with no prior declaration
  int  foo ( int i) {
       ^

t.cpp(4): warning #1011: missing return statement at end of non-void function "foo"
  }
  ^

t.cpp(2): remark #593: variable "b" was set but never used
       int b ;
           ^

$ icpc t2.cpp -c -w3
t2.cpp(3): remark #1418: external function definition with no prior declaration
  int  foo ( int i) {
       ^

t2.cpp(4): remark #593: variable "b" was set but never used
       int b ;
           ^

$ diff t.cpp t2.cpp
0a1,2
> #pragma warning (disable:1011)
>
$
 

Harris__John
Beginner
187 Views

Thank you for responding.  This does work, but what I discovered is that if you try to use it around template code with #pragma warning (push) and (pop), then it doesn't disable the warning for the ultimate instantiations of the template.  It does work if I locate every instantiation of the template and bracket all that code with push/disable/pop.

jimdempseyatthecove
Black Belt
187 Views

Try:

if constexpr (sizeof...(args) >= 1)
{
  if constexpr (sizeof...(args) == 1)
    {
      return /* variable of some type */;
    }
    else
    {
      return /* variable of another type */
    }
}
return /* variable of acceptable type */

 

Harris__John
Beginner
187 Views

Thanks, Jim, but that doesn't work if you actually instantiate the template.

Judith_W_Intel
Employee
187 Views

This is a known limitation of using this pragma. Template instantiations aren't done until after parsing the entire source file so only the pragmas that were in effect at the end of the file will be applied.