Topic: Alias Templates as Template Template arguments


Author: E Allen <allen.eric.m@gmail.com>
Date: Sun, 4 Mar 2012 14:24:15 -0800 (PST)
Raw View
I saw some threads from prior to the final C++11 standard that seemed
to indicate that this would be legal. Now that things are finalized,
is the following, in fact, legal?

namespace MyLib {
template<typename T> class Normal_Traits { /* ... */ };

template<typename T, typename Traits = Normal_Traits<T>>
class Nifty_New_Container { /* ... */ };

template<template<typename> Con> class Widget { Con<int> ci; Con<void*> cp;
 /* ... use ci and cp in member functions */ };
}

template<typename T> using Adapter = MyLib::Nifty_New_Container<T>;

void foo() {
Widget<Adapter> w;
// use w
}


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]




Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?=<daniel.kruegler@googlemail.com>
Date: Mon, 5 Mar 2012 11:18:40 -0800 (PST)
Raw View
On 2012-03-04 23:24, E Allen wrote:
>  I saw some threads from prior to the final C++11 standard that seemed
>  to indicate that this would be legal. Now that things are finalized,
>  is the following, in fact, legal?
>
>  namespace MyLib {
>  template<typename T>   class Normal_Traits { /* ... */ };
>
>  template<typename T, typename Traits = Normal_Traits<T>>
>  class Nifty_New_Container { /* ... */ };
>
>  template<template<typename>   Con>   class Widget { Con<int>   ci; Con<void*>   cp;

I assume the 'class' keyword inserted before Con.

>    /* ... use ci and cp in member functions */ };
>  }
>
>  template<typename T>   using Adapter = MyLib::Nifty_New_Container<T>;
>
>  void foo() {
>  Widget<Adapter>   w;

I assume you mean MyLib::Widget<Adapter>.

>  // use w
>  }

This example is well-formed according to C++11 given the suggested
fixes. You can use an alias template as an argument for a template
template parameter with the same signature. It doesn't matter what the
underlying signature of the aliased type is. In fact, it is possible to
wrap a non-class type via an alias template as template-template
parameter, e.g. adding to your example

template<typename T>
using Funny = T;

we can instantiate in foo():

MyLib::Widget<Funny>  w2;

Now the Widget<Funny>  class contains a data member ci of type int and a
data member cp of type void*.

HTH&  Greetings from Bremen,

Daniel Kr   gler



--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]