samedi 15 janvier 2022

How to return different types according to constexpr parameter in C++ 11?

Example code:

class A {
// ......
}

class B {
// ......
}


// type of var1 is A
constexpr auto var1 = function_or_macro("A");


// type of var2 is B
constexpr auto var2 = function_or_macro("B");

// compile error
// constexpr auto var3 = function_or_macro("Other");

Can I write a template function or macro function_or_macro() to achive this goal in C++11?

The input parameter is always constexpr, I think the compiler can determain the return value type at compile time in this case. But I can't found a way to write the correct code.

Note: Can not use function_or_macro<A>("A").

Aucun commentaire:

Enregistrer un commentaire