C++11 includes a wonderful amount of great features when it comes to type deduction and management altogether. For example auto and decltype -keywords have proven themselves to be a worthy addition to the language.
As I adopted these simple, yet effective features, I begin to think of implementing some kind of reflection system. Here’s what I’ve managed to pull off this far:
/// ------------------------------------------------------------
/// @class Reflection
/// @brief General-purpose reflection class.
/// @exmpl Get type id:
/// auto a = Reflection::get_id_type<int>();
/// auto b = Reflection::get_id_type<Object>();
/// Get type via received id:
/// decltype(Reflection::get_type(a)) d;
/// @note It is forbidden to create an instance of this class.
/// ------------------------------------------------------------
class Reflection{
public:
/// Static member functions:
template<typename T>
static inline long get_id_type(void){
return reinterpret_cast<long>(&Database<T>::id);
}
static auto get_type(long const type_id) -> decltype(/* UNFINISHED! */){ // This is where I'm having problems.
// This function body is intentionally left empty.
// All that matters is the return type.
}
private:
/// Inner structures:
template<typename T>
struct Database{
static void* id; // Created void pointer here, because only the address of this variable matters.
};
/// Constructors & destructors:
Reflection(void) = delete;
~Reflection(void) = delete;
/// Member functions (overloaded operators):
Reflection& operator=(Reflection&&) = delete;
Reflection& operator=(Reflection const&) = delete;
};
The code should be easy enough to understand. If you read the overall code with the comments, you should realize how to use this class. But the question is:
“How do I return an expression from function “get_type” in order to
turn this expression into usable type by decltype-specifier?”
Thanks in advance.
You can’t have a return type that depends on the runtime value of an argument. Period. What you’re trying to do is not feasible.