I encountered a problem when I tried to compile my program on GCC and am interested in knowing how to portably initialize an inherited POD struct using C++11's initializing syntax e.g. struct { int a; int b} my_struct {1, 2}
. The problem can be represented minimally with the following code which compiles fine on MSVC.
#include <iostream>
template <typename A>
struct base {
A a;
};
template <typename A>
class derived : public base<A> {
public:
derived();
};
template <typename A>
derived<A>::derived() : base {1} {
std::cout << "Constructed " << a << std::endl;
}
int main() {
derived<int> d1;
}
However, GCC with C++14 enabled roughly states that derived<A>
does not have any field 'base'. So I try the following change.
template <typename A>
derived<A>::derived() : base<A> {1} {
std::cout << "Constructed " << a << std::endl;
}
GCC now recognizes base<A>
as a field of derived<A>
fine, but states that a
is not declared in this scope.
Funnily enough this change now doesn't compile on MSVC and states "'{': missing function header (old-style formal list?)".
At this point I have no idea how to write this in a standard compliant way.
Thanks.