避免最烦人的解析 [英] Avoid the most vexing parse
本文介绍了避免最烦人的解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何让编译器创建临时文件,在不定义函数的情况下在其上使用默认构造函数?
How do I get the compiler to create temporaries, use the default constructor on them while not defining a function?
struct B {};
template<class T> struct C {};
template<class T,class T1>
struct A
{
A(const T& t,const T1& t1): m_t(t),m_t1(t1)
{
std::cout << __PRETTY_FUNCTION__ << "\n";
}
T m_t;
T1 m_t1;
};
int main() {
A< B , C<B> > a0( B() , C<B>() ); // Function definition
A< B , C<B> > a1( B b , C<B> c ); // dito, *at least A(const T& t,const T1& t1) not called
}
推荐答案
您可以将参数之一包装在额外的括号中,以防止将其解析为函数声明:
You can wrap one of your arguments in an extra set of parentheses to stop it from being parsed as a function declaration:
A< B , C<B> > a0( (B()) , C<B>() );
或更妙的是,如果您可以访问C ++ 11编译器,请使用大括号初始化:
Or even better, if you have access to a C++11 compiler, use brace initialization:
A< B , C<B> > a0{ B() , C<B>() };
这篇关于避免最烦人的解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文