Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 推断模板的运算符/函数的返回类型_C++_Templates - Fatal编程技术网

C++ 推断模板的运算符/函数的返回类型

C++ 推断模板的运算符/函数的返回类型,c++,templates,C++,Templates,这样的事情可能吗 // We can even assume T and U are native C++ types template<typename T, typename U> magically_deduce_return_type_of(T * U) my_mul() { return T * U; } /我们甚至可以假设t和u是本机C++类型。 模板 神奇地推导出(T*U)my\U mul(){return T*U;} 或者有人必须破解一个返回类型结构并为每对本机

这样的事情可能吗

// We can even assume T and U are native C++ types
template<typename T, typename U>
magically_deduce_return_type_of(T * U) my_mul() { return T * U; }
<代码> /我们甚至可以假设t和u是本机C++类型。 模板 神奇地推导出(T*U)my\U mul(){return T*U;} 或者有人必须破解一个返回类型结构并为每对本机类型专门化它吗?

听说过吗

C++0x
中,您可以

template<class T, class U>
auto mul(T x, U y) -> decltype(x*y)
{
    return x*y;
}
模板
自动多路传输(TX,UY)->数据类型(x*y)
{
返回x*y;
}
C++0x之前的版本

我不知道你想要完成什么,所以:

template<typename T, typename U>
void my_mul(T t, U u, bool& overflow) 
{
    my_mul_impl(t*u, overflow);
}

template<typename TmultU>
void my_mul_impl(TmultU mult, bool& overflow)
{
    //here you know the type and can do something meta-weird :)
    if(mult > type_traits<TmultU>::max_allowed_in_my_cool_program())
       overflow = true;
}
模板
作废我的mul(T T、U U、bool和overflow)
{
my_mul_impl(t*u,溢出);
}
模板
作废我的副本(TmultU mult、bool和overflow)
{
//在这里,您知道类型,可以做一些奇怪的事情:)
if(mult>type_traits::max_allowed_in_my_cool_program())
溢出=真;
}

有一个

我正在使用VisualStudio2008,所以我不得不想出一种非C++0x的方法。我最后做了这样的事

template<typename T> struct type_precedence { static const int value = -1; };
template< > struct type_precedence<long double> { static const int value = 0; };
template< > struct type_precedence<double> { static const int value = 1; };
template< > struct type_precedence<float> { static const int value = 2; };
template< > struct type_precedence<unsigned long long> { static const int value = 3; };
template< > struct type_precedence<long long> { static const int value = 4; };
template< > struct type_precedence<unsigned long> { static const int value = 5; };
template< > struct type_precedence<long> { static const int value = 6; };
template< > struct type_precedence<unsigned int> { static const int value = 7; };
template< > struct type_precedence<int> { static const int value = 8; };
template< > struct type_precedence<unsigned short> { static const int value = 9; };
template< > struct type_precedence<short> { static const int value = 10; };
template< > struct type_precedence<unsigned char> { static const int value = 11; };
template< > struct type_precedence<char> { static const int value = 12; };
template< > struct type_precedence<bool> { static const int value = 13; };

/////////////////////////////////////////////////////////////////////////////////////////

template<typename T, typename U, bool t_precedent = ((type_precedence<T>::value) <= (type_precedence<U>::value))>
struct precedent_type { 
    typedef T t; 
};
template<typename T, typename U>
struct precedent_type<T,U,false> { 
    typedef U t;
};

/////////////////////////////////////////////////////////////////////////////////////////

template<typename T, typename U>
typename precedent_type<T,U>::t my_mul() { return T * U; }
模板结构类型_优先级{static const int value=-1;};
模板<>struct type_priority{static const int value=0;};
模板<>struct type_priority{static const int value=1;};
模板<>struct type_priority{static const int value=2;};
模板<>struct type_priority{static const int value=3;};
模板<>struct type_priority{static const int value=4;};
模板<>struct type_priority{static const int value=5;};
模板<>struct type_priority{static const int value=6;};
模板<>struct type_priority{static const int value=7;};
模板<>struct type_priority{static const int value=8;};
模板<>struct type_priority{static const int value=9;};
模板<>struct type_priority{static const int value=10;};
模板<>struct type_priority{static const int value=11;};
模板<>struct type_priority{static const int value=12;};
模板<>struct type_priority{static const int value=13;};
/////////////////////////////////////////////////////////////////////////////////////////

模板您可以在非
C++0x
代码中执行此操作:

template<typename T, typename U> class Mul
{
  T t_;
  U u_;
public:
  Mul(const T& t, const U& u): t_(t), u_(u) {}
  template <class R>
  operator R ()
  {
    return t_ * u_;
  }
};

template<typename T, typename U>
Mul<T, U> mul(const T& t, const U& u)
{
  return Mul<T, U>(t, u);
}
模板类Mul
{
T!;
U__;
公众:
Mul(常数T&T,常数U&U):T(T),U(U){
模板
运算符R()
{
返回t*u;
}
};
模板
Mul Mul(持续T&T、持续U&U)
{
返回Mul(t,u);
}
用法: chart=3; 短u=4; int r=mul(t,u)


这里我们有两种类型的扣除。我们根据用法隐式声明返回类型,不完全是decltype(T*U)

如果没有C++0x,这种情况可能发生吗?@Chris:no!此功能在MSVC++[2010]和g++(4.3版及更高版本)中可用。不,这是添加“auto”和“decltype”项的原因之一,是为了解决此缺陷,该缺陷导致了诸如泛型编程和必须猜测复杂类型(如容器迭代器)等方面的问题。非标准运算符“typeof”存在于一些编译器中以支持此功能,但现在为了方便起见,它已被标准化。您能否提供一个如何使用my_mul()的示例?您可以让编译器通过引用函数来推断传递变量以存储结果的结果类型:
mult(result_double3,float3,double3)
。语法不方便,但可以节省手工黑客的时间。
template<typename T, typename U> class Mul
{
  T t_;
  U u_;
public:
  Mul(const T& t, const U& u): t_(t), u_(u) {}
  template <class R>
  operator R ()
  {
    return t_ * u_;
  }
};

template<typename T, typename U>
Mul<T, U> mul(const T& t, const U& u)
{
  return Mul<T, U>(t, u);
}