Próbuję rozszerzyć klasę bazową o niektóre elementy danych i dlatego potrzebuję dodatkowych argumentów konstruktora oprócz argumentów konstruktora, których moja klasa bazowa potrzebuje. Chcę przekazać pierwsze argumenty konstruktora do klasy bazowej. Oto, co starałem:Jak wyprowadzić i dodać nowe argumenty do podstawowej wersji konstruktora?
#include <string>
#include <utility>
struct X
{
X(int i_) : i(i_) {}
int i;
};
struct Y : X
{
template <typename ...Ts> // note: candidate constructor not viable:
Y(Ts&&...args, std::string s_) // requires single argument 's_', but 2 arguments
//^ // were provided
: X(std::forward<Ts>(args)...)
, s(std::move(s_))
{}
std::string s;
};
int main()
{
Y y(1, ""); // error: no matching constructor for initialization of 'Y'
// ^~~~~~
}
Jednak kompilator (dzyń 3.8, C++ 14 Tryb) pluje następujące komunikaty o błędach na mnie (główne komunikaty są również w powyższym kodzie źródłowym do czytania wygodę):
main.cpp:23:7: error: no matching constructor for initialization of 'Y'
Y y(1, "");
^~~~~~
main.cpp:13:5: note: candidate constructor not viable: requires single argument 's_', but 2 arguments were provided
Y(Ts&&...args, std::string s_)
^
main.cpp:10:8: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided
struct Y : X
^
main.cpp:10:8: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided
1 error generated.
Dlaczego clang próbuje mi powiedzieć, że mój szablonowy konstruktor ma tylko jeden argument, mimo że liczba argumentów jest variadic? Jak mogę to rozwiązać?
można dodać parametr na początku konstruktora? 'Ts && ...' na początku listy parametrów funkcji/konstruktora nie jest wydedukowanym kontekstem ... –
@ W.F. Dlaczego nie jest to kontekst dedukcji szablonu? Kiedy można to wydedukować? –
@ W.F. Tak, kompiluje się, kiedy umieszczam dodatkowe argumenty przed pakietem argumentów "Ts && ...". Ale chciałbym, żeby przyszli po plecaku. –