Two things I wish C++ had.

posted in Tocs' Blog
Published August 28, 2013
Advertisement
I thought I'd share some thoughts on two features I wished C++ had, tell me what you think.


Identifier Template Parameters



I'd really like to be able to take an identifier token as a template parameter, similar to a macro but it could only accept a single identifier.

You could use it somewhat like this.template void AddOne (T &value){ ++value.variablename;}
ortemplate class NamedPair{public: T1 first; T2 second; ...pair like functions...};NamedPair pair;pair.A = 10;pair.B = 20;
You may be wondering why I would want such a feature. While there are more uses the one that prompted me to wish for this was a sort of "Compile Time Reflection" set of helpers. Similar to those in type_traits, but to check if a type has certain functions or variables.

You wouldn't need a macro to create SFINAE member checking classes anymore.template class has_member{ ... };class A{public: int foo;};class B{};has_member::value; //truehas_member::value; //false
Which means you could use std::enable_if to turn on and off functions based of the contents of a template parameter. Which would be really nifty.

You would probably need the equivalent of typedef for identifiers (alias?) if you were to use this in a more complex template situation something like...identdef foo bar;class A{public: int foo;};A.bar = 10;
Which could introduce a whole host of issues... maybe I haven't thought it all the way through.

Perfect Forwarding Without Templates



C++11 added R-Value references and move semantics which ease a lot of the pain from C++03. However to take advantage of this you need to handle forwarding R-Value and L-Value references properly. Which either means using a template or dealing with the permutation explosion of arguments.void A (const int &value){ ... }void A (int &&value){ ... }//ortemplate void A (T &&value){ ... }
Writing out the cases is ok if you have just one argument, but if you have more than one its time to use a template. But with a template you lose the ability to assert which type you would like to accept.template void B (T1 &&one, T2 &&two){ ... }
If I wanted to make sure B only excepted type "Foo" I could only use static asserts to inform the programmer. Instead I'd love to be able to write something likevoid B (Foo &@one, Foo &@two){ ... }
And have the compiler generate the permutations, just as the compiler does for template type && arguments. (Except enforcing type Foo). I chose an @ sign at random, its kind of ugly substitute what you wish.
0 likes 2 comments

Comments

Krohm

Uhm, I'm not quite sure about the identifier thing, but I try to stay away from templates as much as I can so I guess I cannot appreciate those things in full.

August 29, 2013 06:24 AM
Servant of the Lord

Templates are exceptionally useful, as long as they aren't overly-used. There is quite a bit of fighting against non-simple templates to make them work, but once a templated function or class works, it works very well.

August 29, 2013 04:40 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement