I am trying to get a feel for using "if constexpr( expr )" and perform actions if a particular type is detected.
I am tinkering with the following bit:
#include <iostream>
#include <type_traits>
struct aT {};
struct bT {};
struct cT {};
constexpr aT atype;
constexpr bT btype;
constexpr cT ctype;
int main()
{
if constexpr (std::is_same<decltype(atype), aT>::value)
std::cout << "I am type " << typeid(atype).name() << std::endl;
else if constexpr (std::is_same<decltype(btype), bT>::value)
std::cout << "I am type " << typeid(btype).name() << std::endl;
else if constexpr (std::is_same<decltype(ctype), cT>::value)
std::cout << "I am type " << typeid(ctype).name() << std::endl;
}
I get no compile errors/warnings. However, on execution, none of the if constexpr tests evaluate to true.
If anyone might shine a bit of light on this I'd appreciate it.
constexpr
implies const
in the context of variable declaration. Try this:
if constexpr (std::is_same<decltype(atype), const aT>::value)
std::cout << "I am type " << typeid(atype).name() << std::endl;