Idiomatic C++11 type promotion

后端 未结 2 1900
无人共我
无人共我 2021-02-02 00:01

There is a great paper on C++ for scientific computing where the author (T. Veldhuizen) suggests a traits-based approach to address type promotion. I have used such app

2条回答
  •  忘掉有多难
    2021-02-02 00:25

    I think you can use decltype for this:

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

    Or with declval:

    #include 
    template 
    void product() 
    {
        std::cout << typeid(decltype(std::declval() * std::declval())).name() << std::endl;
    }
    

    EDIT For T ans = T(a) * T(b); you can just use auto, auto ans = T(a) * T(b);

提交回复
热议问题