处理条件编译的多个条件的最佳方法

时间:2017-10-31 20:11:01

标签: c++ templates c++14 sfinae

我有一个模板类,其中有一个方法,模板参数决定了该方法的输入和输出,如下所示:

template <typename In, typename Out>
class Foo
{
    Out fn(const In& in)
    {
        Out out;
        return out;
    }
}

所以我尝试了这一点,但在尝试对voidIn使用Out时出现了(可能很明显)错误。所以我尝试添加多个方法,这些方法是这个主题的变体,希望它们的替换能够启用相关的功能并禁用无效的功能:

template <std::enable_if_t<std::is_void<InputType>::value>* = nullptr>
OutputType fn()
{
    OutputType out;
    return out;
}

template <std::enable_if<(!std::is_void<OutputType>::value) && (!std::is_void<InputType>::value)>* = nullptr>
OutputType fn(InputType& t)
{
    OutputType out;
    return out;
}

template <std::enable_if<std::is_void<OutputType>::value>* = nullptr>
void fn(InputType& t)
{}

让我回到&#34;无效的提及无效&#34;领土,或与碰撞签名。

我应该如何优雅地处理这些条件,以便只从模板中创建以下签名之一:

/*In == void && Out != void*/
Out fn(/* no input here to keep compiler happy*/) { return Out; }

/*In != void && Out != void, standard case*/
Out fn(const In& in) { return Out; }

/*In != void && Out == void*/
void fn(const In& in) { /* No returns here to keep compiler happy*/; }

2 个答案:

答案 0 :(得分:5)

FooIn中的任何一个无效时,您可以使用partial specialisation来提供Out的实施,当两者都无效时,您可以使用explicit specialisation

语法如下(注意Foo后的尖括号表示这是主要Foo类模板的特化

template<typename Out>
struct Foo<void, Out>  // specialisation for only In = void
{ ... };

template<typename In>
struct Foo<In, void>   // specialisation for only Out = void
{ ... };

template<>
struct Foo<void, void> // specialisation for both In and Out = void
{ ... };

以下是一个例子:

#include <iostream>

// primary class template
template <typename In, typename Out>
struct Foo {
    Out fn(const In& in) { return Out{}; }
};

// partial specialisation for when In=void
template<typename Out>
struct Foo<void, Out> {
    Out fn() { return Out{}; }
};

// partial specialisation for when Out=void
template<typename In>
struct Foo<In, void> {
    void fn(const In& in) { }
};

// explicit specialisation for when both In=void and Out=void
template<>
struct Foo<void, void> {
    void fn() { }
};

int main() {
    Foo<int, double> f;
    f.fn(5);

    Foo<void, void> g;
    g.fn();

    Foo<void, int> h;
    h.fn();

    Foo<int, void> i;
    i.fn(5);

    return 0;
}

答案 1 :(得分:1)

返回值和输入值是不同的问题,您可以独立解决它们。

首先,我建议您切换模板类型的顺序:第一个Out,下一个In

这是因为如果在变量输入类型列表中转换In

template <typename Out, typename ... Ins>
struct Foo { /* ... */ };

您自动解决void输入类型(您根本不表达它)。

不是很好的解决方案,因为输入值很难使用;只是为了向您展示如何创建一个适用于所有情况的结构/方法。

对于返回void类型,您只需执行

即可
return (Out)someVal;

其中someVal的类型为OutOut不是void,而另一种类型(例如:intOutvoid

因此,如果您按如下方式定义类型特征deVoid

template <typename T>
struct deVoid
 { using type = T; };

template <>
struct deVoid<void>
 { using type = int; }; // a fake not-void type

template <typename T>
using deVoid_t = typename deVoid<T>::type;

您可以按如下方式定义out变量

  deVoid_t<Out> out {};
以这种方式

return

  return (Out)out;

Outvoid时也有效。

所以你可以按如下方式写Foo

template <typename Out, typename ... Ins>
struct Foo
 {
   Out fn (Ins const & ... ins)
    {
      deVoid_t<Out> out {};

      return (Out)out;
    }
 };

适用于零输入类型(ex void输入类型)和void返回类型。

以下是一个完整的工作示例

template <typename T>
struct deVoid
 { using type = T; };

template <>
struct deVoid<void>
 { using type = int; }; // a fake not-void type

template <typename T>
using deVoid_t = typename deVoid<T>::type;

template <typename Out, typename ... Ins>
struct Foo
 {
   Out fn (Ins const & ... ins)
    {
      deVoid_t<Out> out {};

      return (Out)out;
    }
 };

int main ()
 {
   Foo<int, int>         f; f.fn(42);
   Foo<void>             g; g.fn();
   Foo<int>              h; h.fn();
   Foo<void, int>        i; i.fn(42);
   Foo<void, int, long>  j; j.fn(42, 84L);
 }
相关问题