【发布时间】:2013-06-30 16:46:53
【问题描述】:
来自上一个问题:
Doing a static_assert that a template type is another template
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;
}
这很好用。
但如果我将这样的代码更改为使用 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 std::true_type { };
template<typename T>
struct foo {};
//Added: alias for foo
template<typename T>
using foo_alt = foo<T>;
template<typename FooType>
struct bar {
//Changed: want to use foo_alt instead of foo here
static_assert(is_instantiation_of<foo_alt,FooType>::value, ""); //fail
};
int main(int,char**) {
//both of these fail:
bar<foo<int>> b;
bar<foo_alt<int>> b2;
return 0;
}
这可以解决吗?
【问题讨论】:
-
嗯,看起来
foo_alt是 typedef-name 而不是 template-name...但这只会影响您的 @987654327 @;你可以保持main不变。 -
我认为您可以检查两种类型是否是同一模板的实例化,这也适用于别名模板。