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

C++17 constexpr if

Johannes_P_
Beginner
1,103 Views

Hi @ all,

I am using constexpr if widely within my code. After a successfull build with the gcc I wanted to use *the good stuff*, namely icpc.

Here is a short example of my code:

/*...*/
  virtual inline void run( ) override {
    #pragma omp parallel for
      for( std::size_t i = 0; i < REP_COUNT; ++i ) {
        //...
        if constexpr( ( PROCESS_DATA_COUNT / 4 ) >= 2 ) {
          //...
        }
        if constexpr( ( PROCESS_DATA_COUNT / 4 ) == 4 ) {
          //...
        }
      }
  }

/*...*/

When I try to compile it with the icpc (Version 18.0.0.128 Build 20170811), it breaks with the following output:

/usr/include/c++/7/bits/stl_pair.h(79): error: inline specifier allowed on function declarations only
    _GLIBCXX17_INLINE constexpr piecewise_construct_t piecewise_construct =
    ^

/usr/include/c++/7/utility(345): error: inline specifier allowed on function declarations only
    inline constexpr in_place_t in_place{};
    ^

 ... error: expected a "("
            if constexpr( ( PROCESS_DATA_COUNT / 4 ) >= 2 ) {
               ^

Is there no constexpr if within the newest icpc? Is there a proper workaround?

 

Sincerely yours

 

 

 

0 Kudos
5 Replies
Olga_M_Intel
Employee
1,103 Views

'consexpr if' is not listed in Supported Features- https://software.intel.com/en-us/articles/c17-features-supported-by-intel-c-compiler

 

0 Kudos
Steven_K
Beginner
1,103 Views

It might not be listed but the relevant question is when it is going to get implemented.

If I remember correctly, it has been stated that version 18 won't support constexpr-if at all, so we have to wait until next year.
This is unfortunate (for Intel) because it's one of the most asked features. And yes there are workarounds (Which I prefer not to use):
template specialization, enable_if and so on.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,103 Views

FWIW

Even without "if constexpr(...)" any good compiler optimization when given "if(expression that is constant at compile time)" should be able to optimize out the code section(s) that will never be entered.

Why invent a new syntax that is not needed?

Jim Dempsey

0 Kudos
Johannes_P_
Beginner
1,103 Views

Is it for sure, that the "invalid" branch will never be compiled? For instance if I have two lets called it "flavors" of a method, depending on some template parameter, i can use SFINAE to realize the behaviour I want. But it's a bit smelly I think. 

0 Kudos
simmse
Beginner
1,103 Views

To help the rest of us, the official C++17 document number is P0292R2.  It would be great to know whether the next update to 18.0 will support compile time if (if constexpr).  Does anyone have a response from Intel?

0 Kudos
Reply