小编B.D*_*B.D的帖子

是否从模板模板参数UB生成唯一ID?

我正在尝试从模板模板参数生成唯一的 ID。我尝试过这个功能

inline size_t g_id = 1;

template<template<typename> typename T>
inline size_t GetID()
{
    static size_t id = g_id++;
    return id;
}
Run Code Online (Sandbox Code Playgroud)

在与别名模板一起使用之前它工作正常

template<template<typename> typename T>
inline void print()
{
    std::cout << GetID<T>() << "\n";
}

template<typename T>
struct S {};
struct W1 { template<typename A> using type = S<A>; };
struct W2 { template<typename A> using type = S<A>; };

int main()
{
    print<S>();
    print<W1::type>();
    print<W2::type>();

    std::cin.get();
}
Run Code Online (Sandbox Code Playgroud)

MSVC

1
2
3
Run Code Online (Sandbox Code Playgroud)

1
2
3
Run Code Online (Sandbox Code Playgroud)

海湾合作委员会

1
1 …
Run Code Online (Sandbox Code Playgroud)

c++ templates language-lawyer type-alias c++17

8
推荐指数
1
解决办法
551
查看次数

标签 统计

c++ ×1

c++17 ×1

language-lawyer ×1

templates ×1

type-alias ×1