• Advertisement
Sign in to follow this  
  • entries
    4
  • comments
    7
  • views
    8947

Two things I wish C++ had.

Sign in to follow this  

1217 views

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.
Sign in to follow this  


2 Comments


Recommended Comments

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.

Share this comment


Link to comment

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.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement