Reputation: 8352
I have the following class definition:
template<typename T>
class Point {
private:
T px, py;
public:
Point(T x, T y): px(x), py(y) {
std::cout << "created " << x << ":" << y <<std::endl;
};
T x() const { return px; };
T y() const { return py; };
};
from which I am deriving specializations, e.g.
class PointScreen: public Point<int> {
using Point::Point;
};
When I compile this in clang++
, I get no warning / error, but the constructor is not called:
#include <iostream>
// definitions from above
int main() {
std::cout << PointScreen(100, 100).x() << std::endl;
return 0;
}
This returns a random value (and also not the debug output "created..."). The value returned by e.g. x()
is obviously "undefined".
I have just tried the same in g++
here, and there I obtain the expected result. Is this a problem with clang++
or have I a bug in my code?
My clang version: Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0). I compile with -std=c++11 -Wall
.
Upvotes: 4
Views: 1222
Reputation: 70506
As pointed out in the comments, you need a compiler supporting inheriting constructors. From the Apache C++11 overview you can see that this feature is only available for gcc >= 4.8 and Clang >= 3.3.
For older compilers, you have to manually define all the constructors, by calling the base constructors. See also this Q&A for more details on work-arounds.
Upvotes: 1