Question

Suppose I have a template:

template<typename T>
struct Foo {int f1, f2;};

I want to create a new alias for it.

// This will not work, don't even try:
// using Foo = Bar;

// Instead do like this:
template<typename T>
using Bar = Foo<T>;

WOW. It seems to work. At first. But then... If I have function like this:

// Generic f:
template<template<typename> class Tpl>
void f() {std::cout<<"Generic f"<<std::endl;}

// Specialization of f for Foo:
template<> void f<Foo>() {std::cout<<"f<Foo>"<<std::endl;}

int main() {...; f<Bar>(); ...} //outputs "Generic f"

It appears that f<Foo> and f<Bar> are different specializations of f!

So:

  1. Is this GCC 4.8 bug, or C++11 standard design flaw, or it is expected to be so?
  2. Is there way to create "real" template alias in C++11? In C++14? C++17?

Thank you.

Was it helpful?

Solution

Is this GCC 4.8 bug, or C++11 standard design flaw, or it is expected to be so?

This is expected to be so. Actually, ironically, the Standard (draft) initially contained examples that suggested examples like yours to work. But the normative text didn't allow that and the example was revised to not suggest this anymore.

See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1244

Is there way to create "real" template alias in C++11? In C++14?

No. It was decided that this is a very special case of the general alias template and if at all this should be solved with a cleaner syntax that directly expresses the alias, when the above linked issue was discussed. Alias templates ultimately yield types (and are templated over types/"typedefs") and are not "template aliases" like they were called at some point in early drafting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top