上下文:我正在尝试编写表达式模板和 C++11 功能。附加的代码示例只是一个有趣的实验。在 ET 的这种变体中,每个表达式都跟踪自己的返回类型。common_type
然后编译器使用它来查找基于子表达式返回类型的其他表达式的返回类型。
问题:您可以在此处查看完整示例
我有一组函数可以动态计算返回类型,common_type
如下所示:
template
<
typename... Args2,
typename T1,
template < typename... > class T2
>
Binary
<
T1,
T2< Args2... >,
typename common_type< T1 , typename select_last< Args2... >::type >::type
> const
operator*(T1 u, T2< Args2... > v)
{
cout << "Operator (value, expr)" << endl;
return Binary
<
T1,
T2< Args2... >,
typename common_type< T1 , typename select_last< Args2... >::type >::type
>(u, v);
}
编译时
clang++ -std=c++11 -O3 -Wall -pedantic -Wextra main.cpp -lboost_iostreams -lz
一切正常。当使用编译时,clang++ -stdlib=libc++ -std=c++11 -O3 -Wall -pedantic -Wextra main.cpp -lcxxrt -ldl -lboost_iostreams -lz
我得到构建错误,其中非基元被传递到common_type
. (又名incompatible operand types ('Unary<int, int>' and 'int')
)
问题:是否匹配了错误的功能?即使在未使用的功能中,似乎common_type
也可以进行评估。有没有一种简单的方法来延迟common_type
两个终端表达式运算符的评估?