In C # 4.0, you can use the keyword "dynamic" as a placeholder for a type that is unknown before execution. There are certain angular cases when this is extremely useful behavior. Is it possible to emulate something like this in C ++, possibly using the C ++ 0x or RTTI functions?
Not really. The closest you can get is void *, but you still need to give it to the appropriate type before you can use it.
void *
Update:
DSL, , ++.
:
,
struct MyType { enum { NUMBER, STRING /* etc */ } type; union { double number; string str; }; };
class MyType { public: /* define pure virtual operations common to all types */ }; class MyNumber : public MyType { private: double number; public: /* implement operations for this type */ };
# dynamic .NET. ++ , . RTTI , . , .
dynamic
, , , , .
: .
dynamic d = /* something */; d.Foo(bar); // Foo is unknown at compile time
.NET dynamic , , , , ( ). , , . , - , TMP , , runtime.
, - :
dynamic d = /* something */; d.invoke("Foo", bar);
@Trillian ( BTW) , , , , dynamic, CLR - , , dynamic, (, COM IDispatch). ++, , () , dynamic ++ ( , ).
IDispatch
. , . , . # , .
, . (, ), , , , , .
, . ++ 0x auto, , , ++ , :
auto
template<typename T> T square(const T& someArg){ return T*T; }
: . , , . , () . union, boost::variant
union
boost::variant