Jump to content

April 2017 »

232425262728 29

Recent Comments

- - - - -

Two things I wish C++ had.

4: Adsense

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)

template <class T1, identifier first, class T2, identifier second>
class NamedPair
    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
    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
     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)
{ ... }


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.

Aug 29 2013 12:24 AM

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.

Aug 29 2013 10:40 AM

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.

Note: GameDev.net moderates comments.