pointer as non-type template argument

后端 未结 2 736
青春惊慌失措
青春惊慌失措 2020-12-03 05:08

When answering this SO question, I found in the Standard (already C++03, still in C++11) that you can only use addresses as non-type template arguments if they\'re of the fo

相关标签:
2条回答
  • 2020-12-03 05:30

    Consider classes Foo<&X> and Foo<&Y>, both with a static member int Bar. The linker must be able to tell whether your program has 1 or 2 Bar objects. Now consider that the linker is also the party most likely responsible for assigning values to &X and &Y.

    Look at the Standard again. As it's written, the compiler doesn't need to communicate the actual address to the linker. Instead, it passes the id-expression. Linkers are already quite capable of determining whether two id-expression's are the same, even before assigning a numerical address to them.

    0 讨论(0)
  • 2020-12-03 05:30
    1. It cannot be a variable, since variables are only set at runtime.
    2. It cannot be a constexpr, since the value of an address cannot be known at compile time; in most cases it will only be fixed after relocation prior to execution.
    3. It might theoretically be an arithmetic expression (even though it isn't allowed in the standard), but in the common case of an address of an array element, you can simply use &arr[i] instead of arr + i.
    0 讨论(0)
提交回复
热议问题