Context: I am playing around with writing expression templates and C++11 features. The attached code sample is just an experament for fun. In this variation of ETs, each expression keeps track of its own return type. common_type
is then used by the compiler to find what the return types of other expressions would be based on the sub-expression return types.
Problem: You can see the complete example here
I have a set of functions that dynamically figure out the return type using common_type
like so:
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);
}
When compiled with
clang++ -std=c++11 -O3 -Wall -pedantic -Wextra main.cpp -lboost_iostreams -lz
everything works fine. When compiled with clang++ -stdlib=libc++ -std=c++11 -O3 -Wall -pedantic -Wextra main.cpp -lcxxrt -ldl -lboost_iostreams -lz
I get build errors where non-primitives are being passed in to common_type
. (aka incompatible operand types ('Unary<int, int>' and 'int')
)
Question: Is the wrong function being matched? It seems like common_type
may be evaluated even in the functions not being used. Is there an easy way to delay the evaluation of common_type
for the two terminal expression operators?