std::conditional compile-time branch evaluation
Posted
by
cmannett85
on Stack Overflow
See other posts from Stack Overflow
or by cmannett85
Published on 2014-06-07T14:35:18Z
Indexed on
2014/06/07
15:24 UTC
Read the original article
Hit count: 263
Compiling this:
template < class T, class Y, class ...Args >
struct isSame
{
static constexpr bool value = std::conditional<
sizeof...( Args ),
typename std::conditional<
std::is_same< T, Y >::value,
isSame< Y, Args... >, // Error!
std::false_type >::type,
std::is_same< T, Y > >::type::value;
};
int main()
{
qDebug() << isSame< double, int >::value;
return EXIT_SUCCESS;
}
Gives me this compiler error:
error: wrong number of template arguments (1, should be 2 or more)
The issue is that isSame< double, int >
has an empty Args
parameter pack, so isSame< Y, Args... >
effectively becomes isSame< Y >
which does not match the signature.
But my question is: Why is that branch being evaluated at all? sizeof...( Args )
is false
, so the inner std:conditional
should not be evaluated. This isn't a runtime piece of code, the compiler knows that sizeof..( Args )
will never be true
with the given template types.
If you're curious, it's supposed to be a variadic version of std::is_same
, not that it works...
© Stack Overflow or respective owner