Does the c++11 standard say anything about templated unions? (I can't find anything in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf, but I haven't read it that carefully.)
I have
template<typename T>
union u {
T a;
char b;
};
template<typename T>
u<T> make_u(T t) {
return { .a = t };
}
int main() {
return make_u<int>(1).a;
}
This code causes icpc -std=c++11
to say error: a designator into a template-dependent type is not allowed
, g++ -std=c++0x
to say error: expected primary-expression before ‘.’ token
, and g++ -std=c++11
(version 4.8.0 (experimental)) to say internal compiler error: in lookup_field_1, at cp/search.c:387
. I can get around this by replacing { .a = t }
with t
. However, I could not do this for fields which are not the first member of the union. Is there a way to pick some member other than the first one in a templated union, where the relevant member is template-dependent? (I could, of course, declare a union on the stack, and set the member equal to the value I want. But I could not do this in an initializer list or in a constexpr
function.)