模板别名的相等 [英] Equality of template aliases

查看:96
本文介绍了模板别名的相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建无法与原始别名区分开的模板别名.

I try to create template alias which cannot be distinguished from original.

因此,我创建了特征以检查2个模板(非类型)是否相等:

So, I create traits to check when 2 templates (not types) are equal:

template <template <class...> class C1,
          template <class...> class C2>
struct is_same_template : std::false_type {};

template <template <class...> class C1>
struct is_same_template<C1, C1> : std::true_type {};

现在进行测试:

// Expected alias
template <typename ... Ts> using V_Ts = std::vector<Ts...>;    // Variadic
// Fallback alias
template <typename T, typename A> using V = std::vector<T, A>; // Exact count

static_assert(!is_same_template<std::vector, V_Ts>::value); // Alias rejected by gcc/clang
static_assert( is_same_template<std::vector, V>::value);    // Alias accepted only for gcc

演示

是否可以创建"true"别名? 哪个编译器是正确的?

Is it possible to create "true" alias? which compiler is right?

推荐答案

我尝试创建无法与原始别名区分开的模板别名.

I try to create template alias which cannot be distinguished from original.

我认为目前这是不可能的. (不幸的是)没有模板别名,只有别名模板.别名模板始终是其自己的 [temp.alias] /1 .别名模板的特殊化等同于您通过将模板参数替换为别名模板而获得的类型,但是别名模板本身不是另一个模板的别名

I don't think this is currently possible. There are (unfortunately) no template aliases, there are only alias templates. And an alias template is always a template of its own [temp.alias]/1. A specialization of an alias template is equivalent to the type you get by substituting the template arguments into the alias template, but the alias template itself is not an alias for another template [temp.alias]/2. I would consider GCC letting your second static_assert pass a bug in GCC…

如@HolyBlackCat在上面的评论中所指出的,有一个相关的问题和答案,它指向许多相关的CWG问题.特别是一个问题( CWG 1286 )建议希望在某些情况下允许别名模板本身等同于它所引用的模板.但是,由于稍后提出的关注,似乎未通过所提议的决议.当前标准草案中的相关措词( [temp.alias] [temp.type] )与C ++ 11似乎没有什么关系.

As pointed out by @HolyBlackCat in the comment above, there is a related question and answer which points to numerous related CWG issues. One issue in particular (CWG 1286) would seem to suggest that there is desire to allow an alias template to itself be equivalent to the template it refers to under certain circumstances. However, it does not seem that the proposed resolution has been adopted due to concerns raised later. The relevant wording in the current standard draft ([temp.alias] and [temp.type]) appears to be unchanged from C++11…

这篇关于模板别名的相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆