Alias of a template. Who's right?

梦想的初衷 提交于 2020-05-13 04:47:12

问题


The following code seems reasonable, but doesn't work on two major compilers

#include <type_traits>

template<template<class> class Tmp>
struct S{
    template<class T>
    using tmp_t = Tmp<T>;

    static_assert(std::is_same_v< S<tmp_t>, S<Tmp> >,
        "Not same?? How come?");
};

template<class> struct Dummy{};

template struct S<Dummy>;

gcc starting with 7.1 compiles alright ( https://godbolt.org/z/DjAcgP )

clang ( https://godbolt.org/z/ewBbZJ )
and
msvc ( https://godbolt.org/z/6ZmQwj )
fail to do so

Is this code standard conformant?


回答1:


GCC is wrong. tmp_t declares a new template (an alias template to be precise). And this new template is distinct from any other template.

[temp.alias] (emphasis mine)

1 A template-declaration in which the declaration is an alias-declaration declares the identifier to be an alias template. An alias template is a name for a family of types. The name of the alias template is a template-name.

The text in bold means that tmp_t refers to the new alias template, it does not refer to whatever the alias' specializations may be defined as. As such S<tmp_t> and S<Tmp> are two specializations with different arguments.

To contrast, there are special rules that make alias template specializations stand in for the exact thing they alias

2 When a template-id refers to the specialization of an alias template, it is equivalent to the associated type obtained by substitution of its template-arguments for the template-parameters in the type-id of the alias template.

So the while template-id tmp_t<foo> means exactly the same thing as Tmp<foo>, tmp_t itself (without arguments) is not a template-id (it does not name a specialization). Instead it names the template, a different entity.




回答2:


Even though, as it turns out, I don't need it (yet) in my case, it is possible to circumvent this defect, but only if the passing around of the template template parameter is only done by the developer and not the user.

Wrap the template in another type... :P

I want to say right away: I don't think it's a good idea; code that uses that would be too complicated, and probably because it utilizes abnormal ideas.

https://godbolt.org/z/GgJY8r

the example is really poor and far-fetched. All because of the wrong set of mind.

But I just wanted to put it out there...



来源:https://stackoverflow.com/questions/58782399/alias-of-a-template-whos-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!