Skip to content

Instantly share code, notes, and snippets.

@nthery
Created March 24, 2022 15:29
Show Gist options
  • Save nthery/1c0a7a2a6a7f187337429d69a211dc65 to your computer and use it in GitHub Desktop.
Save nthery/1c0a7a2a6a7f187337429d69a211dc65 to your computer and use it in GitHub Desktop.
// Templated constructor in derived class.
#include <utility>
struct B {
B();
B(int);
B(const B&);
B(B&&);
int payload[24];
};
struct D : B {
D();
D(double);
D(const D&);
D(D&&);
template<class ...Ts>
D(Ts&&... args)
: B(std::forward<Ts...>(args)...) {}
int more_payload[42];
};
void f() {
// Calls D::D()
D d0;
// Calls D::D(int)
D d1{42};
// Calls D::D(const D&) not the templated constructor.
D d2{d1};
// Calls D::D(double)
D d3{3.14};
// Calls D::D<float>, not D::D(double)
D d4{3.14f};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment