I have two classes, A and B, and B is derived from A.
I'm trying to use perfect forwarding, so I don't have to rewrite all the constructor overloads of A in class B. However, the following scenario leads to an error:
#include <utility>
#include <string>
struct A {
A(const std::string& a) : name(a) {}
A(){}
virtual ~A(){}
std::string name;
};
struct B : public A {
template<typename... Args>
B(Args&&... args) : A(std::forward<Args>(args)...) {}
B(const std::string& a, int b) : A(a), p(b) {}
int p = 0;
};
int main()
{
B b1("foo");
B b2("foobar", 1);
}
For b2, GCC complains about no matching function for call to 'A::A(const char [5], int)
. Apparently it is trying to call the perfect forwarding constructor, which obviously shouldn't work, instead of the second constructor of B.
Why doesn't see the compiler the second constructor and call that instead? How can I fix this behaviour?
The exact error message:
main.cpp: In instantiation of 'B::B(Args&& ...) [with Args = {const char (&)[5], int}]':
main.cpp:26:19: required from here
main.cpp:15:54: error: no matching function for call to 'A::A(const char [5], int)'
B(Args&&... args) : A(std::forward<Args>(args)...) {}
^
main.cpp:6:5: note: candidate: A::A()
A(){}
^
main.cpp:6:5: note: candidate expects 0 arguments, 2 provided
main.cpp:5:5: note: candidate: A::A(const string&)
A(const std::string& a) : name(a) {}
^
main.cpp:5:5: note: candidate expects 1 argument, 2 provided
main.cpp:4:8: note: candidate: A::A(const A&)
struct A {
^
main.cpp:4:8: note: candidate expects 1 argument, 2 provided
Aucun commentaire:
Enregistrer un commentaire