jwd
jwd

Reputation: 11144

Clang bug? Using pointer as template argument

I want to use a pointer as a template argument. Generally, this works fine. But further, I want those pointers to be declared via another template class.

I have it working fine on GCC, but it is failing on Clang.

Here is a small program to demonstrate (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;
}

The error is:

error: non-type template argument of type 'char **' is not a constant expression

As you can see from the more thorough example at the live link, clang handles it fine when it's a plain global pointer, and when it's a pointer inside a plain struct. It's only when using a pointer inside a templated struct that it has trouble.

Upvotes: 0

Views: 83

Answers (1)

H Krishnan
H Krishnan

Reputation: 916

I guess the issue is with thread_local. If I add thread_local to pData as well, it fails to compile.

Upvotes: 1

Related Questions