Two things I wish C++ had.
2,034
2
Advertisement
I thought I'd share some thoughts on two features I wished C++ had, tell me what you think.
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.
or
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.
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...
Which could introduce a whole host of issues... maybe I haven't thought it all the way through.
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.
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.
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 like
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.
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;}or
template 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; //falseWhich 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 like
void 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.
Advertisement
Advertisement
Advertisement
Discussion