14

一篇关于 C++ 用于科学计算的精彩论文,其中作者 (T. Veldhuizen) 提出了一种基于特征的方法来解决类型提升问题。我使用过这种方法,发现它很有效:

#include<iostream>
#include<complex>
#include<typeinfo>

template<typename T1, typename T2>
struct promote_trait{};

#define DECLARE_PROMOTION(A, B, C) template<> struct promote_trait<A, B> { using T_promote = C;};

DECLARE_PROMOTION(int, char, int);
DECLARE_PROMOTION(int, float, float);
DECLARE_PROMOTION(float, std::complex<float>, std::complex<float>);

// similarly for all possible type combinations...

template<typename T1, typename T2>
void product(T1 a, T2 b) {
  using T = typename promote_trait<T1, T2>::T_promote;
  T ans = T(a) * T(b);  
  std::cout<<"received "
           <<typeid(T1).name()<<"("<<a<<")"<<" * "
           <<typeid(T2).name()<<"("<<b<<")"<<" ==> "
           <<"returning "
           <<typeid(T).name()<<"("<<ans<<")"<<std::endl;
}

int main() {
  product(1, 'a');
  product(1, 2.0f);
  product(1.0f, std::complex<float>(1.0f, 2.0f));
  return 0;
}

输出:

received i(1) * c(a) ==> returning i(97)
received i(1) * f(2) ==> returning f(2)
received f(1) * St7complexIfE((1,2)) ==> returning St7complexIfE((1,2))

typeinfo 返回的类型名称取决于实现;您的输出可能与我的不同,后者在 OS X 10.7.4 上使用 GCC 4.7.2

本质上,该方法定义了一个promote_trait只包含一个类型定义的类型:当以给定方式操作时,两种类型应该提升到的类型。需要声明所有可能的促销活动。

当一个函数同时接收这两种类型时,它依赖于promote_trait推断出正确的、提升的结果类型。如果没有为给定的对定义特征,则代码无法编译(一个理想的特性)。

现在,有问题的论文写于 2000 年,我们知道 C++ 在过去十年中发生了巨大的变化。那么,我的问题如下:

是否有一种现代的、惯用的 C++ 11 方法来处理类型提升,与 Veldhuizen 引入的基于特征的方法一样有效?

编辑(关于使用std::common_type

根据 Luc Danton 的建议,我创建了以下代码,它使用std::common_type

#include<iostream>
#include<complex>
#include<typeinfo>
#include<typeindex>
#include<string>
#include<utility>
#include<map>

// a map to homogenize the type names across platforms
std::map<std::type_index, std::string> type_names = {
  {typeid(char)                 , "char"},  
  {typeid(int)                  , "int"},
  {typeid(float)                , "float"},
  {typeid(double)               , "double"},
  {typeid(std::complex<int>)    , "complex<int>"},
  {typeid(std::complex<float>)  , "complex<float>"},
  {typeid(std::complex<double>) , "complex<double>"},
};

template<typename T1, typename T2>
void promotion(T1 a, T2 b) {
  std::string T1name = type_names[typeid(T1)];
  std::string T2name = type_names[typeid(T2)];
  std::string TPname = type_names[typeid(typename std::common_type<T1, T2>::type)];  
  std::cout<<T1name<<"("<<a<<") and "<<T2name<<"("<<b<<") promoted to "<<TPname<<std::endl;
}

int main() {
  promotion(1, 'a');
  promotion(1, 1.0);
  promotion(1.0, 1);
  promotion(std::complex<double>(1), 1);
  promotion(1.0f, 1);
  promotion(1.0f, 1.0);
  promotion(std::complex<int>(1), std::complex<double>(1));
  promotion(std::complex<double>(1), std::complex<int>(1));
  promotion(std::complex<float>(0, 2.0f), std::complex<int>(1));

  return 0;
}

输出:

int(1) and char(a) promoted to int
int(1) and double(1) promoted to double
double(1) and int(1) promoted to double
complex<double>((1,0)) and int(1) promoted to complex<double>
float(1) and int(1) promoted to float
float(1) and double(1) promoted to double
complex<int>((1,0)) and complex<double>((1,0)) promoted to complex<int>
complex<double>((1,0)) and complex<int>((1,0)) promoted to complex<int>
complex<float>((0,2)) and complex<int>((1,0)) promoted to complex<int>

我惊讶地发现,除了最后三个促销活动之外,所有促销活动都符合我的预期。为什么会complex<int>complex<double>complex<float>被提升为complex<int>!?

4

2 回答 2

5

正如 catcradle 的答案decltypecommon_type(以及它的自定义特化)一样,可能是很好的 C++11 替代品,可以满足 Veldhuizen 考虑的转换特征的需求。但是,如果您仍然需要非常具体地评估将两种类型映射为一种(二元运算符)的函数,那么它仍然存在不足。(换句话说decltype,不知道你的问题的数学领域)。

我的看法是你可以求助于 Boost.MPL 地图http://www.boost.org/doc/libs/1_53_0/libs/mpl/doc/refmanual/map.html,这甚至不需要 C++11 ,只是当时没有写MPL:

#include<iostream>
#include<complex>
#include<typeinfo>
#include <boost/mpl/map.hpp>
#include <boost/mpl/at.hpp>
// all traits in one place, no need for MACROS or C++11, compile error if the case does not exist.
using namespace boost::mpl;
typedef map<
    pair<pair<int, char>, int>,
    pair<pair<int, float>, int>,
    pair<pair<float, std::complex<float> >, std::complex<float> >
> mapped_promotion;

template<typename T1, typename T2>
void product(T1 a, T2 b) {
  typedef typename at<mapped_promotion, pair<T1, T2> >::type T;

  T ans = T(a) * T(b);  
  std::cout<<"received "
           <<typeid(T1).name()<<"("<<a<<")"<<" * "
           <<typeid(T2).name()<<"("<<b<<")"<<" ==> "
           <<"returning "
           <<typeid(T).name()<<"("<<ans<<")"<<std::endl;
}

int main() {
  product(1, 'a');
  product(1, 2.0f);
  product(1.0f, std::complex<float>(1.0f, 2.0f));
  return 0;
}

使用 MPL 的另一个额外好处是您可以轻松地移到Boost.Fusion后面,这通常是您开始处理类型的“代数”时的情况。并且 C++11 核心语言中没有什么可以替代 Boost.Fusion 的功能。


下面是一个更通用的解决方案,如果以上内容对您的应用程序来说已经足够了,您可以停止阅读,它结合了 MPL 并且decltype需要 C++11,它允许未指定的类型对默认为 decltype 解决方案(如果有任何好处),诀窍是查看返回的mpl::map是否是元类型void_(未找到对)。

...
#include <type_traits> 
//specific promotions
using namespace boost::mpl;
typedef map<
    pair<pair<int, char>, int>,
    pair<pair<int, float>, int>,
    pair<pair<float, std::complex<float> >, std::complex<float> >
> specific_mapped_promotion;

//promotion for unspecified combinations defaults to decltype type deduction.
template<class P1, class P2>
struct loose_mapped_promotion : std::conditional<
    std::is_same<typename at<specific_mapped_promotion, pair<P1, P2> >::type, mpl_::void_>::value,
    decltype( std::declval<P1>()*std::declval<P2>() ),
    typename at<specific_mapped_promotion, pair<P1, P2> >::type
> {};
template<typename T1, typename T2>
void product(T1 a, T2 b) {
  typedef typename loose_mapped_promotion<T1, T2>::type T;

  T ans = T(a) * T(b);
  ...
}
int main() {
   product(1.0, std::complex<double>(1.0f, 2.0f)); // now accepted, although no explicit trait was made
}

最后一点:对于特殊情况,显然可以重载std::common_type,如果你想使用它:http ://www.cplusplus.com/reference/type_traits/common_type/

于 2013-05-31T20:58:05.593 回答
3

我想你可以用decltype这个:

template <typename T, typename U>
void product(T t, U u) 
{
    std::cout << typeid(decltype(t * u)).name() << std::endl;
}

或与declval

#include <utility>
template <typename T, typename U>
void product() 
{
    std::cout << typeid(decltype(std::declval<T>() * std::declval<U>())).name() << std::endl;
}

编辑因为T ans = T(a) * T(b);你可以使用autoauto ans = T(a) * T(b);

于 2013-05-31T20:29:30.897 回答