Avoid rewriting type to access member typedef

Started by
7 comments, last by NightCreature83 8 years, 10 months ago

I often find myself doing something like


some::name::space::Foo<Bar> test;
some::name::space::Foo<Bar>::SomeTypedef stuff = test.hi();

Now obviously I dont want to retype the type... In this specific case I could just use 'auto' but this is just an example (maybe I need the type to pass it as a template parameter to a vector or whatever).

Id like to do


some::name::space::Foo<Bar> test;
test::SomeTypedef stuff = test.hi();

But thats ILLEGAL sad.png

Is typedef the only way to do this in C++ (apart from duplication)? (Its fine for many uses of the type, but if I use the type like twice, writing out the typedef just feels like it takes even more work)

EDIT:

This would save at least a few keystrokes and eliminate the need to move stuff around, yay! But its only worth it if theres multiple places where the behavior is needed because ill need to #include the header with the macro T_T


#define WITH_ALIAS(type, alias) typedef type alias; type

//Rarely do macros, dont know if that even works xD EDIT2: seems to...

WITH_ALIAS(some::name::space::Foo<Bar>, fubar) test;
test::SomeTypedef stuff = test.hi();

o3o

Advertisement

I've not used much of the most recent versions of C++, but the "using" keyword's recent upgrade might be worth looking into:

namespace some {
  namespace name {
    namespace space {
       template <typename T> struct Foo {
         typedef int SomeTypedef;
         SomeTypedef hi() { return 42; }
       };
    }
  }
}
 
struct Bar {};
 
int main() {
  using fubar = some::name::space::Foo<Bar>;
  fubar test;
  fubar::SomeTypedef stuff = test.hi();
}

There is also decltype, but it looks funky:

struct Bar {};
 
int main() {
  some::name::space::Foo<Bar> test;
  decltype(test)::SomeTypedef stuff = test.hi();
}


decltype(test)::SomeTypedef stuff = test.hi();

I actually tried doing this but the compiler in VS 2013 doesnt like that... Looks like it should work though :I

o3o

I tried both clang and g++.


decltype(test)::SomeTypedef stuff = test.hi();

I actually tried doing this but the compiler in VS 2013 doesnt like that... Looks like it should work though :I

I just tried in VS2013, and although Intellisense produces the red squiggles, the compiler seems to handle it without complaint. Those red squiggles would drive me insane, though.

"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

Yup, seems to compile fine.


Those red squiggles would drive me insane, though.

And theres not only one, theres three. Everywhere I use the variable whose type depends on the decltype...

Ill use it anyways, VS will not be allowed oppress my elegant C++11 coed! angry.png

o3o

Why not create class to inherit that object which declares the type? How many different types do you plan on wrapping? Abstracting the problem(typedef) would make it more manageable and is probably a more sound OO approach. You may thank yourself in the future for reducing complexity. Sorry this is one of those "screw your idea" replies but I think your time is more valuable elsewhere. In c# the using simplifies it like Rip-Off stated but I think it's a feature added later, so maybe you have a version issue.

Why not create class to inherit that object which declares the type? How many different types do you plan on wrapping? Abstracting the problem(typedef) would make it more manageable and is probably a more sound OO approach. You may thank yourself in the future for reducing complexity. Sorry this is one of those "screw your idea" replies but I think your time is more valuable elsewhere. In c# the using simplifies it like Rip-Off stated but I think it's a feature added later, so maybe you have a version issue.

Because there's no need to abuse the type system like that.

Waterlimon, I believe VS2013 has support for type aliases as described by rip-off.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight


decltype(test)::SomeTypedef stuff = test.hi();

I actually tried doing this but the compiler in VS 2013 doesnt like that... Looks like it should work though :I

VS2013 is not a C++11 compliant compiler, so features are missing from this version, VS2015 will be closer but even that might not be a full version. The only full versions at the moment are gcc and Clang.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement