4
template<int const * pci> struct X {};
extern int const ai[];
X<ai> xi;
int const ai[] = {0,1,2,3};

If I try to compile this code with "clang++ -std=c++1z" it result in error:

test.cpp:4:3: error: non-type template argument refers to subobject '&ai'

But it isn't subobject.

http://en.cppreference.com/w/cpp/language/template_parameters doesn't list any suitable limitation for extern arrays in '(since C++17)' section for non-type arguments.

Such code works fine with -std=c++14. And GCC also compiles it without errors in c++1z mode: https://godbolt.org/g/K9wZ4g

Is it a clang bug? Or is this code wrong?

4
  • 4
    "But it isn't subobject." Yes, it is a subobject. An array in C++ can decay into a pointer to the first subobject element in the array. Now, that's not the say that the compiler should fail for that. But it is getting a pointer to a subobject. Commented Jul 1, 2016 at 13:52
  • 2
    @NicolBolas The fun part about this is that [temp.arg.nontype]/1 bans pointers to subobjects, and then the next paragraph has an example that matches the OP's. Commented Jul 1, 2016 at 18:55
  • Now I'm totally confused. I've reread what standard means by subobject and yes, array element is a subobject. Looks like there is nothing in the standard which allows using arrays as pointer non-type template arguments. Not only extern but any arrays. Commented Jul 1, 2016 at 19:50
  • 1
    CWG 2043 seems to be related. Commented Jul 2, 2016 at 10:35

1 Answer 1

3

Yes, it's a clang bug, confirmed by clang developer and has been fixed in trunk (r311970). http://lists.llvm.org/pipermail/cfe-dev/2017-August/055249.html

Meanwhile, to work around it, you need to write the length of array in the declaration explicitly.

extern int const ai[4];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.