for*_*818 7 c++ templates template-templates language-lawyer
上下文
这个问题询问是否可以编写一个可以从给定实例中检索模板的特征。简而言之,问题是:“是否有可能写出f这样f<foo<int>>::type的foo?”
这个答案提出了一些我本来期望的正是那个问题所要求的东西,但答案已经提到它“不完全是”所要求的。
编码
为了查看运行中的代码并理解为什么它被标记为“不完全”,我写了这个:
#include <type_traits>
#include <iostream>
// the template
template <typename T> struct foo {};
// not the template
template <typename T> struct not_foo {};
template <template<class> class A, template<class> class B>
struct is_same_template : std::false_type {};
// this is the questionable part
template <template<class> class A>
struct is_same_template<A,A> : std::true_type {};
// copied from said answer
template <typename T> struct template_class;
template <template <typename> class C, typename T>
struct template_class<C<T>>
{
template <typename U>
using type = C<U>;
};
// what will we get?
int main (){
std::cout << is_same_template<foo,foo>::value;
std::cout << is_same_template<foo,not_foo>::value;
std::cout << is_same_template<foo, template_class<foo<int>>::type>::value;
}
Run Code Online (Sandbox Code Playgroud)
输出
我所期待的,gcc:
101
Run Code Online (Sandbox Code Playgroud)
我的惊喜,叮当声:
100
Run Code Online (Sandbox Code Playgroud)
问题
谁在这里?Gcc,因为template_class< foo<int> >::type是foo? 或者叮当声,因为比较两个不同的平等模板没有多大意义?
如果结果是实现定义的或未定义的,那么代码中究竟什么是未定义/实现定义的?
聚苯乙烯
起初我毫无疑问地期待 gcc 的结果,但同时我确信我的期望并不完全成立,因此提出了这个问题。
| 归档时间: |
|
| 查看次数: |
58 次 |
| 最近记录: |