Let’s say I have the class Foo. The following works fine:
class Foo
{
public:
const int* bar;
Foo()
{
bar = new int[2] {1, 2};
}
};
However, I tried to change this very slightly to use a template:
template<int A, int B>
class Foo
{
public:
const int* bar;
Foo()
{
bar = new int[2] {A, B};
}
};
My understanding of the way templates work is that A and B are essentially compile time constants, so it should still work the same.
The error message I get when compiling with g++ (4.5 in the link, same error with 4.6.3) is:
error: ISO C++ forbids initialization in array new [-fpermissive]
With 4.7 a similar error occurs, though slightly different:
error: parenthesized initializer in array new [-fpermissive]
The problem also occurs in template functions, and not just when template parameters are used within the braces for initialization, code and output. (thanks Philipp)
Looks like this is a GCC bug. Clang accepts it, and the standard allows it:
And the rules for this initialization are not special: