在模板参数列表中跳过较早的隐式模板参数,但不以后 [英] Skip earlier implicit template parameter, but not later, in a template parameter list

查看:46
本文介绍了在模板参数列表中跳过较早的隐式模板参数,但不以后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇:

在下面的示例中,我显示了代替/* implicit */的意思.有没有解决方法可以将其留空?如您所见,类型名称T不能是第一个.

On the following sample I show what I mean in place of /*implicit*/. Is there a workaround to leave it blank? typename T cannot be first as you can see.

template<typename C1, typename C2, typename T = decltype(typename C1::value_type() * typename C2::value_type())>
T dot(const C1 &v1, const C2 &v2);

int main()
{
    std::vector<float> vec1;
    std::vector<double> vec2;
    // typical:
    auto result1 = dot(vec1, vec2); // auto -> double
    // avoid numerical unstable situations:
    auto result2 = dot</*implicit*/,/*implicit*/,long double>(vec1, vec2); // auto -> long double
    //auto result2 = dot<decltype(vec1),decltype(vec2),long double>(vec1, vec2);
}

在最后一行中,我提供了一个解决方案,它不是超级膨胀的.

In the last line, I provide a solution witch is not super-bloated.

推荐答案

只需重新排列参数,然后有条件:

Just reorder the parameters, and go conditional:

// short notation for `std::declval<typename C::value_type&>()`
template<class C>
typename C::value_type& value_in();

// if T == void, find actual result type, else use T
template<class T, class C1, class C2>
using DotResult = typename std::conditional<std::is_void<T>::value,
    decltype(value_in<C1>() * value_in<C2>()), T>::type;

// void can't be a valid value_type
template<class T = void, class C1, class C2>
DotResult<T,C1,C2> dot(C1 const& c1, C2 const& c2);

但是,我不得不承认...我真的看不到它的用途-您可以直接转换结果,或隐式转换它... long double x = dot(v1,v2);

However, I have to admit... I really don't see the use for it - you could just cast the result instead, or implicitly convert it... long double x = dot(v1, v2);

这篇关于在模板参数列表中跳过较早的隐式模板参数,但不以后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆