0

I learnt that under certain conditions, pointers can also be non-type template parameters. Then I wrote the following program that compiles with msvc but not with gcc and clang. Live Demo

template<typename T, T*>
struct S{};

int main() {
   S<int, (int*)2> s4;  //msvc compiles this while gcc and clang reject this  
}

As we can see, the program works with msvc but not with gcc and clang and I don't know which compiler is correct here as per the C++ standard.

Note this is purely for academic purposes.

Alan
  • 116
  • 9
  • 1
    Dup of [reinterpret\_cast(37)' is not a constant expression](https://stackoverflow.com/questions/54174694/reinterpret-castvolatile-uint8-t37-is-not-a-constant-expression) – Language Lawyer Nov 30 '22 at 15:31
  • Or, better [constexpr and initialization of a static const void pointer with reinterpret cast](https://stackoverflow.com/questions/24398102/constexpr-and-initialization-of-a-static-const-void-pointer-with-reinterpret-cas) – Language Lawyer Nov 30 '22 at 15:32
  • 1
    in c++20 you can't do it anymore as a cast from integer to pointer is not a const expression anymore. For me it feels like a tragic language bug as it kills the usage of memory maps provided by controller vendors which are typically something like `(volatile uint8* REG_YXY)=0x2345;` This makes it impossible to me to use c++ anymore on AVR. See here https://stackoverflow.com/questions/67037386/how-to-create-a-constexpr-pointer-to-a-register-on-embedded-system/74616533#74616533 – Klaus Nov 30 '22 at 15:34

0 Answers0