After looking at many examples of metaprogramming in C ++ that allow you to find out if the properties of classes (for example, know whether a type is a specialization of a template ) or knowing whether a class includes this nested type; but I was wondering if it is possible to write a test or a sign that determines the last one, to verify that this Type nested in a class or struct .
In other words, I am looking for the equivalent of the following pseudocode:
template <typename Type> struct is_nested { enum { value = {__some magic__} }; }; typedef int type1; struct Something { typedef int internal_type; }; typedef Something::internal_type type2; //...later, likely at a different scope is_nested< int >::value; // yields false is_nested< std::vector<int>::iterator >::value; // yields true is_nested< type1 >::value; // yields false is_nested< type2 >::value; // yields true
I know that I can use sizeof to implement yes / no tests, and I assume that Type is part of these tests, but I cannot figure out how to connect some kind of βany viable typeβ to the test so that I can form an expression, similar to Anytype::Type .
template
struct is_nested
{
typedef char yes;
typedef struct {char u [2]; } no;
// Herein lies the problem
???? static yes test (char [sizeof (Anytype :: Type)]);
???? static no test (...);
public:
enum {value = sizeof (test (0)) == sizeof (char)};
};
(Note that I do not care, and (I can afford it) to know what type the Type will be nested, all that matters is if it is nested by something or not. In other words, this symptom should depend only on Type .)
I am looking for a solution in C ++ in C ++ 11 or C ++ 03, but in the first case I would have welcomed it much more if it was backportable.
source share