c++ - Is "#define TYPE(x) typename decltype(x)" a bad idea? -


is bad idea define

#define type(x) typename decltype(x) 

as fast way member type of class of variable in c++11?

justification:

consider following (oversimplified) example:

#include <iostream> #define type(x) typename decltype(x)  class { public:   typedef int mtype;   void set(mtype const& x) { foo = x; }   mtype get() { return foo; } private:   mtype foo; };  make() { return a(); }  int main() {   auto = make();   type(a)::mtype b = 3;   a.set(b);   std::cout << a.get() << "\n";   return 0; } 

that is, have class member type "mtype" , function returns me instance of class. auto, don’t have worry name of class long know function job. however, @ 1 point or another, have define variable of specific type, which, convenience, has typedef "mtype" in class.

if have variable name "a" around know variable has class, can do

typename decltype(a)::mtype b; 

to define variable b of type a::mtype.

is bad idea put "typename decltype" macro? there obvious, common cases in break? there better way @ type?

bonus: "a::mtype" nicest - there reason not defined behave "decltype(a)::mtype" in standard?

yes, bad. use template-using instead:

template<class t> using mtype_t = typename t::mtype; 

usage:

auto = make(); mtype_t<decltype(a)> b = 3; 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -