I have the following struct MyStruct
that calls Foo<int>
on construction:
struct MyStruct {
MyStruct() {
Foo<int>();
}
template<typename T>
void Foo() {
[]() {
struct Base {
Base(int n) {}
};
struct Derived : Base {
// using Base=Base; // needs to be uncommented for Apple LLVM version 9.1.0 (clang-902.0.39.2)
Derived() :
Base(0) // problematic line
{}
} derived;
};
}
};
I tried to compile this with clang (via godbolt, command line params --std=c++1y
):
- 3.6:
error: type 'Base' is not a direct or virtual base of 'Derived'
(seeproblematic line
in code above) - 3.7 and newer: compiles (with a warning about unused expressions)
So I expected that with newer versions this should be ok. However, when I tried to compile it on a Mac using XCode9 (which, according to this, uses clang 4.0 (clang --version
gives Apple LLVM version 9.1.0 (clang-902.0.39.2)
)), I get an error from the same line as issued by clang 3.6:
error: member initializer 'Base' does not name a non-static data member or base class
As a workaround, I tried introducing using Base=Base
within struct Derived
(as indicated), which made it compile via XCode. (As a sidenote: Using the same workaround did not work for clang 3.6 on godbolt.)
So here are my questions:
- Is this a compiler bug? If yes: Is it documented somewhere?
- Is my workaround
using Base=Base
valid, well-defined C++?