Reputation: 97
I am trying to create a meta-function that returns a type. The inspiration came from the first accepted answer in C++ meta-function over templates . I want my meta-function to return the type char
and then I want to use that output to create a char variable. I believe my template works but I have no idea how to check the output. I was unable to print it. How do I check what the returned type is? How do I use that returned type to create a variable?
This is a summarized version of my code:
template<typename T> struct AA;
// allow for shorter syntax - decl<...>
template<typename T> using decl = typename AA<T>::result;
// char termination
template<ExprType eType>
struct AA<Expr<eType>> { using result = char; };
...other templates that specialize and allow for recursion
int main()
{
decl<..many args..> typee;
return 0;
}
Upvotes: 0
Views: 615
Reputation: 63114
My usual method is to declare a dummy class template:
template <class...>
struct check_type;
And use it in a way that triggers an error:
check_type<decltype(typee)>{};
This way the type of whatever I passed appears in the compiler's output:
prog.cc: In function 'int main()':
prog.cc:8:18: error: invalid use of incomplete type 'struct check_type<int>'
8 | check_type<int>{};
| ^
compilation terminated due to -Wfatal-errors.
Upvotes: 1
Reputation: 217085
You can check type with
static_assert(std::is_same_v<char, decl</*..many args..*/>>)
And for debugging, you might use
template <typename> struct Debug; /* No definition */
Debug<decl</*..many args..*/>> d; // Error similar to: No definition for Debug<char>
Upvotes: 1