叮当虫?使用指针作为模板参数
Clang bug? Using pointer as template argument
我想使用指针作为模板参数。一般来说,这很好用。
但更进一步,我希望通过另一个模板 class.
声明这些指针
我在 GCC 上运行良好,但在 Clang 上却失败了。
这里有一个小程序来演示(live link):
template <size_t ID = 0>
struct PointerHolderT {
static thread_local char *smt_pStorage;
};
template <size_t ID>
thread_local char *PointerHolderT<ID>::smt_pStorage = nullptr;
template<char **ppData>
struct MyClass {
static void foo() {
cout << "this is my ptr: " << ppData << endl;
}
};
int main() {
MyClass<&PointerHolderT<123>::smt_pStorage>::foo();
return 0;
}
错误是:
error: non-type template argument of type 'char **' is not a constant expression
正如您从实时 link 上更详尽的示例中看到的那样,当它是一个普通的全局指针时,以及当它是一个普通结构中的指针时,clang 都能很好地处理它。只有在 templated 结构中使用指针时才会出现问题。
- 这是 clang 错误吗?
- 或者我所做的是非法的,但 GCC 允许这样做?
- 有没有人知道解决方法,所以我可以在 clang 上运行它?
我想问题出在 thread_local
上。如果我也将 thread_local
添加到 pData
,它会编译失败。
我想使用指针作为模板参数。一般来说,这很好用。 但更进一步,我希望通过另一个模板 class.
声明这些指针我在 GCC 上运行良好,但在 Clang 上却失败了。
这里有一个小程序来演示(live link):
template <size_t ID = 0>
struct PointerHolderT {
static thread_local char *smt_pStorage;
};
template <size_t ID>
thread_local char *PointerHolderT<ID>::smt_pStorage = nullptr;
template<char **ppData>
struct MyClass {
static void foo() {
cout << "this is my ptr: " << ppData << endl;
}
};
int main() {
MyClass<&PointerHolderT<123>::smt_pStorage>::foo();
return 0;
}
错误是:
error: non-type template argument of type 'char **' is not a constant expression
正如您从实时 link 上更详尽的示例中看到的那样,当它是一个普通的全局指针时,以及当它是一个普通结构中的指针时,clang 都能很好地处理它。只有在 templated 结构中使用指针时才会出现问题。
- 这是 clang 错误吗?
- 或者我所做的是非法的,但 GCC 允许这样做?
- 有没有人知道解决方法,所以我可以在 clang 上运行它?
我想问题出在 thread_local
上。如果我也将 thread_local
添加到 pData
,它会编译失败。