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

bad runtime value passed for constexpr value in c++17

vindriolo
Beginner
1,307 Views

In certain cases, functions that take in const ref values are getting invalid/undefined values at runtime when passing in static constexpr members with -std=c++17.  But the same example fails to link with -std=c++14. 

OS: CentOS Linux release 7.8.2003 (Core)
GCC: g++ (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)
Compiler versions tested:
    icpc (ICC) 19.0.1.144 20181018
    icpc (ICC) 19.0.4.227 20190416
    icpc (ICC) 19.0.4.243 20190416
    icpc (ICC) 19.1.2.254 20200623

Command line:
$ icpc -std=c++17 ./constexpr_test.cpp && ./a.out
a.out: ./constexpr_test.cpp:30: void SomeClass<A>::check(const unsigned int &) [with A = int]: Assertion `key != 0' failed.
Aborted (core dumped)

$ icpc -std=c++14 ./constexpr_test.cpp && ./a.out
/tmp/icpcub4xzI.o: In function `main':
constexpr_test.cpp:(.text+0x2c): undefined reference to `SomeClass<int>::SPECIFIC_KEY'

below is a code example:

#include <cassert>
#include <cstdint>
#include <cstdio>
#include <limits>

// works if SomeClass is not a template
template <typename A>
struct SomeClass
{
  static constexpr uint32_t SPECIFIC_KEY = std::numeric_limits<uint32_t>::max();

  // Uncommenting this works around the issue
  // static_assert(SPECIFIC_KEY != 0, "SPECIFIC_KEY is 0!");

  SomeClass()
  {
    check(SPECIFIC_KEY);

    // Uses safely w/o workarounds, but doesn't fix
    //check(uint32_t(SPECIFIC_KEY));

    // Uncommenting this works around the issue, presumably because it requires a const reference
    // printf("specific key: %d\n", SPECIFIC_KEY);

  }

private:
  void check(const uint32_t& key)
  {
    assert(key != 0);
  }
};

int main(int argc, char** argv)
{
  SomeClass<int> user;
  return 0;
}

 

0 Kudos
6 Replies
RahulV_intel
Moderator
1,282 Views

Hi,


The issue is reproducible with icpc (ICC) 2021.1 Beta 20200827. I have informed this issue to the concerned team. Thanks for reporting this.



Regards,

Rahul


0 Kudos
Viet_H_Intel
Moderator
1,272 Views

I have reported this issue to our Developer.

There is icpx in 19.1.2.254. Can you use it for the time being?

$ icpc -V

Intel(R) C++ Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 19.1.2.254 Build 20200623

Copyright (C) 1985-2020 Intel Corporation. All rights reserved.


$ icpc t.cpp -std=c++17 && ./a.out

a.out: t.cpp:18: void SomeClass<A>::check(const unsigned int &) [with A = int]: Assertion `key != 0' failed.

Aborted (core dumped)

$ icpx t.cpp -std=c++17 && ./a.out

$


Seems like this feature isn't supported in C++14, as both icpc and g++ ( tested on 8.1.0) gave error.

$ icpc t.cpp -std=c++14

/tmp/icpcVyN1zZ.o: In function `main':

t.cpp:(.text+0x28): undefined reference to `SomeClass<int>::SPECIFIC_KEY'

$ g++ t.cpp -std=c++14

/tmp/cc6Wwwqg.o: In function `SomeClass<int>::SomeClass()':

t.cpp:(.text._ZN9SomeClassIiEC2Ev[_ZN9SomeClassIiEC5Ev]+0x11): undefined reference to `SomeClass<int>::SPECIFIC_KEY'

collect2: error: ld returned 1 exit status





0 Kudos
vindriolo
Beginner
1,262 Views

When using icpx, the assert does not hit with -std=c++17.  What is this binary, an experimental build of the compiler?

0 Kudos
Viet_H_Intel
Moderator
1,258 Views

This compiler is based on LLVM technology.

Thanks,


0 Kudos
RahulV_intel
Moderator
1,025 Views

Hi @vindriolo,

 

As pointed out by @Viet_H_Intel, the source code works fine with the ICPX compiler. Let us know if you face any issues.

 

Thanks,

Rahul

 

0 Kudos
PrasanthD_intel
Moderator
984 Views

Hi Vince,


We are closing this thread as we haven't heard back from you, assuming your issue has been resolved. We will no longer respond to this thread. If you require additional assistance from Intel, please start a new thread. Any further interaction in this thread will be considered community only.


Regards

Prasanth


0 Kudos
Reply