我正在尝试从模板模板参数生成唯一的 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)