Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

c++ - Alias of a template. Who's right?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...