Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.






Two things I wish C++ had.

Posted by Tocs, 28 August 2013 · 725 views

C++
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 <class T, identifier variablename>
void AddOne (T &value)
{
    ++value.variablename;
}

or
template <class T1, identifier first, class T2, identifier second>
class NamedPair
{
public:
    T1 first;
    T2 second;

    ...pair like functions...
};

NamedPair<int,A,int,B> 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 T, identifier member>
class has_member
{ ... };

class A
{
public:
    int foo;
};

class B
{
};

has_member<A,foo>::value; //true
has_member<B,foo>::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)
{ ... }

//or

template <class T>
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 <class T1, class T2>
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.




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.

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.

December 2014 »

S M T W T F S
 123456
78910111213
14151617 18 1920
21222324252627
28293031   

Recent Comments

PARTNERS