模板函数重载中的MSVC2015 decltype参数类型

罗斯·本西纳

以下程序兼容C ++ 11吗?如果是这样,您是否知道触发它的特定MSVC错误?和/或可能的解决方法?

#include <iostream>

struct A {};
struct B {};

constexpr A aaa = {};
constexpr B bbb = {};

template <typename T>
void foo(T, decltype(aaa)) { std::cout << "a"; }

template <typename T>
void foo(T, decltype(bbb)) { std::cout << "b"; }
// ^ C2995 'void foo(T,unknown-type)': function template has already been defined

int main()
{
    foo(0, aaa);
    foo(0, bbb);
}

如果用实际的类型代替decltype它就可以了,但是在实践中,这些类型太复杂而无法重现,我不希望它们没有别名。

紫罗兰色长颈鹿

经过以下较小修改即可为我工作(VS 2015 / v140):

#include <iostream>

struct A {};
struct B {};

constexpr A aaa = {};
constexpr B bbb = {};

using A_type = decltype(aaa);
using B_type = decltype(bbb);

template <typename T>
void foo(T, A_type) { std::cout << "a"; }

template <typename T>
void foo(T, B_type) { std::cout << "b"; }

int main()
{
    foo(0, aaa);
    foo(0, bbb);
}

但是此变体会产生相同的错误(不确定该怎么做):

template <typename T>
struct TypeWrapper {
    using type = T;
};

template <typename T>
void foo(T, typename TypeWrapper<decltype(aaa)>::type) { std::cout << "a"; }

template <typename T>
void foo(T, typename TypeWrapper<decltype(bbb)>::type) { std::cout << "b"; }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

c ++ 17中的非类型模板参数可以是decltype(auto)吗?

从函数的参数声明容器的decltype

获取模板参数的decltype

递归可变参数函数模板的返回类型的decltype

使用decltype的功能参数类型

具有模板参数的Decltype

在模板参数列表中使用decltype推断指向类成员的指针的类型

如何使用MSVC2015将带有一些自变量的参数包装器放置到容器中?

C ++中函数类型的模板参数

使用模板重载函数类型

模板功能参数类型中的模板标识符与decltype

C ++从decltype返回类型中删除noexcept

vs2015在可变参数模板中找不到重载的成员函数

decltype()可变参数模板基类

非类型模板参数中的占位符类型可以涉及作为模板参数传递的函数的重载解析吗?

模板类型推导如何使用重载函数作为参数

如何使用decltype获取向量元素的类型作为模板参数

为什么模板参数推导在重载函数中失败?

为什么在ctor的参数列表中用`decltype(x)`替换成员`x`的类型会破坏类模板参数推导?

尝试根据模板类型重载模板类中的虚函数

变量模板参数中的Decltype

可以在模板参数中使用decltype吗?

带两个参数的decltype = decltype(a,b),用于函数返回类型

根据参数中向量的类型重载函数

MSVC2015更新3可变参数模板变通办法

成员函数指针的decltype作为c ++ 11中的模板参数

为模板参数中的每种类型声明并实现重载的虚函数

元函数使用 decltype 返回元素类型

使用 decltype 作为构造函数参数