Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

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

Harris__John
Beginner
9,548 Views

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
9,548 Views

you can add -wd1011 to your command line. 

Thanks,

Viet

0 Kudos
Harris__John
Beginner
9,548 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.

0 Kudos
Viet_H_Intel
Moderator
9,548 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)
>
$
 

0 Kudos
Harris__John
Beginner
9,548 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.

0 Kudos
jimdempseyatthecove
Honored Contributor III
9,548 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 */

 

0 Kudos
Harris__John
Beginner
9,548 Views

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

0 Kudos
Judith_W_Intel
Employee
9,548 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.

 

0 Kudos
Reply