Constructor inheritance in a template class (C ++ 11)

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 get specializations for example

 class PointScreen: public Point<int> { using Point::Point; }; 

When I compile this in clang++ , I don't get any warnings / errors, 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 (as well as no debug output "created ..."). The value returned, for example, x() , is obviously "undefined".

I just tried the same in g++ here , and there I get the expected result. Is this a problem with clang++ or am I having a code error?

My clang version: Ubuntu version clang 3.0-6ubuntu3 (tags / RELEASE_30 / final) (based on LLVM 3.0). I am compiling with -std=c++11 -Wall .

+4
source share
1 answer

As pointed out in the comments, you need a compiler that supports constructor inheritance. From the Apache C ++ 11 review, you can see that this function is only available for gcc> = 4.8 and Clang> = 3.3.

For older compilers, you must manually define all the constructors, referencing the base constructors . See also this Q&A for more information on working conditions.

+1
source

Source: https://habr.com/ru/post/1499960/


All Articles