#include <iostream>
class A
{
public:
A(bool b, int i)
: b_(b) , i_(i) {}
void print()
{
std::cout << b_ << " " << i_ << "\n";
}
private:
bool b_;
int i_;
};
class B
{
public:
B(double d)
: d_(d) {}
void print()
{
std::cout << d_ << "\n";
}
private:
double d_;
};
template<class T=A, typename ... Args>
void f(int a, Args ... args)
{
std::cout << a << std::endl;
T t(args...);
t.print();
}
int main()
{
f(1,false,3);
f<A>(2,true,1);
f<B>(3,2.0);
}
上面的代码编译并运行良好。但:
// (class A and B declared as above)
template<class T, typename ... Args>
class F
{
public:
F(int a, Args ... args)
{
std::cout << a << std::endl;
T t(args...);
t.print();
}
};
int main()
{
F<A>(4,true,1);
F<B>(5,2.0);
}
无法使用消息编译:
main.cpp:在函数'int main()'中:main.cpp:64:18:错误:没有匹配函数调用'F :: F(int,bool,int)'F(4,true,1) ;