c++ - 键入特征以从运算符(operator)处获取提升结果

标签 c++ templates typetraits

Implicit type conversion rules in C++ operators

列出 C++ 的隐式类型转换。

那么问题来了,是否有类型特征可以做那个表呢?类似于

template <typename T, typename J>
struct promotion_type
{
  typedef decltype(operator+(const T&,const J&)) type;
};

(这不是我的问题,但这无法编译:

/home/user/source/testdir/main.cpp:97:51: error: there are no arguments to 'operator+' that depend on a template parameter, so a declaration of 'operator+' must be available [-fpermissive]
   typedef decltype(operator+(const T&,const J&)) type;
                                               ^
/home/user/source/testdir/main.cpp:97:51: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)

)

最佳答案

decltype(std::declval<T>() + std::declval<J>())怎么样? ?

关于c++ - 键入特征以从运算符(operator)处获取提升结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22849906/

相关文章:

c++ - 为了模拟对象,我们是否应该将所有成员函数声明为虚拟(C++)?

C++:在不使用 vector 的情况下将字符串数组传递给函数

c++ - 在汇编文件中调用 C++ 函数

arrays - 在字典中列出作为模板定义

c++ - 使用 decltype() 的 SFINAE 在 Visual Studio 2012 上忽略 "private"

c++ - 用于测试任意方法是否存在的 std::is_invocable 语法(不仅仅是 operator())

c++ - float (int) const 的类型特征

c++ - 如何确定感兴趣区域,然后使用 OpenCV 裁剪图像

wpf - 当 WPF TextBox 获得焦点时,如何轻松更改它的样式?

c++ - 调用多参数模板函数