C ++ equivalent of keyword "dynamic" C # 4.0?

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?

+5
source share
5 answers

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.

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 */
};
+4

# dynamic .NET. ++ , . RTTI , . , .

+3

, , , , .

: .

:

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 ++ ( , ).

+1

. , . , . # , .

0

, . (, ), , , , , .

, . ++ 0x auto, , , ++ , :

template<typename T>
T square(const T& someArg){
   return T*T;
}

: . , , . , () . union, boost::variant

-1

All Articles