请考虑以下代码:
#include <type_traits>
template<template<class...> class T, class... U>
struct is_specialization_of : std::false_type{};
template<template<class...> class T, class... U>
struct is_specialization_of<T, T<U...>> : std::true_type{};
template<class T, class U = int>
struct test{};
// (1) ok
static_assert(is_specialization_of<test, test<int>>::value, "1");
template<class T>
using alias = test<T>;
// (2) fails
static_assert(is_specialization_of<alias, alias<int>>::value, "2");
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
为什么(2),即static_assert
使用别名模板,失败?
(2)中的模板参数推导过程与(1)中的模板参数推导过程有何不同?
从上一个问题:
Andy Prowl为我提供了这个代码,允许我static_assert
模板类型是另一种模板类型:
template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of : public std::false_type { };
template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of<TT, TT<Ts...>> : public std::true_type { };
template<typename T>
struct foo {};
template<typename FooType>
struct bar {
static_assert(is_instantiation_of<foo,FooType>::value, ""); //success
};
int main(int,char**)
{
bar<foo<int>> b; //success
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这非常有效.
但是如果我改变这样的代码来使用别名foo
,事情会变得糟糕:
template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of : public std::false_type { };
template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of<TT, TT<Ts...>> : public …
Run Code Online (Sandbox Code Playgroud)