包装非类型模板常量,以避免混合相同类型的参数

时间:2018-11-27 18:00:32

标签: c++ c++11 templates c++14 c++17

我有一个使用非类型模板参数的模板方法。 它具有以下形式:

template <long long connectionTimeout, long long sendTimeout, bool autoAck>
void create() { ... }

这是另一个标头中的实用函数,在调用者的代码中令我烦恼的是未键入常量。

含义,而不是这种调用方式:

create<1, 2, true>();

我更喜欢以下内容:

create<
    connection_timeout {1},
    send_timeout {2},
    auto_ack {true}
>();

使用create函数可确保无法传递send_timeout而不是connection_timeout

但是,我开始写概念证明,但存在一些空白。我想使其与C ++ 11/14一起使用。但是,直到现在为止,我必须使用C ++ 17构造(cf代码)才能使工作正常。话虽这么说,但我不介意C ++ 17解决方案是否可以做到这一点。

以下缺少类型匹配的编译时检查。但是,主要是我希望拥有的语法。

#include <iostream>
#include <string>

template <typename T, T userSpecifiedValue>
struct compile_time_constant_wrapper
{
   using type = T;
   static const T defaultValue = userSpecifiedValue;

   constexpr operator T() const
   {
       return value;
   }

   T value = defaultValue;
};

using connection_timeout = compile_time_constant_wrapper<long long, 5000>;
using send_timeout = compile_time_constant_wrapper<long long, 10>;
using auto_ack = compile_time_constant_wrapper<bool, false>;

struct ComplicatedToBuild
{
    long long connectionTimeout;
    long long sendTimeout;
    bool autoAck;
};

template <typename T, 
          long long connectionTimeout = connection_timeout {} /*-std=c++17*/,
          long long sendTimeout = send_timeout {} /*-std=c++17*/,
          bool autoAck = auto_ack {} /*-std=c++17*/>
struct create
{
    operator T() const
    {
        return T{connectionTimeout, sendTimeout, autoAck};
    }
};

std::ostream& operator<<(std::ostream& out, const ComplicatedToBuild& complicated)
{
    out << "connection timeout = " << complicated.connectionTimeout << ", "
        << "send timeout = " << complicated.sendTimeout << ", "
        << "auto ack = " << complicated.autoAck;
    return out;
}

int main()
{
    ComplicatedToBuild defaultValuesCase = create<ComplicatedToBuild>();
    std::cout << "defaultValuesCase: " << defaultValuesCase << std::endl;

    ComplicatedToBuild customizedCase = create<
           ComplicatedToBuild,
           connection_timeout {2500},
           send_timeout {5},
           auto_ack {true}
    >();
    std::cout << "customizedCase: " << customizedCase << std::endl;

    ComplicatedToBuild compilationErrorCase = create<
           ComplicatedToBuild,
           send_timeout {5},
           connection_timeout {2500},
           auto_ack {true}
    >();
}

就我而言,类ComplicatedToBuild不是普通的结构。并且在编译时就知道了构建它所需的值。这就是为什么我想到使用非类型模板的原因。

2 个答案:

答案 0 :(得分:4)

#include <type_traits>    

enum class connection_timeout : long long {};
enum class send_timeout : long long {};
enum class auto_ack : bool {};

struct ComplicatedToBuild
{
    long long connectionTimeout;
    long long sendTimeout;
    bool autoAck;
};

template <typename T
        , connection_timeout connectionTimeout = connection_timeout{5000}
        , send_timeout sendTimeout = send_timeout{10}
        , auto_ack autoAck = auto_ack{false}>
T create()
{
    return {std::underlying_type_t<connection_timeout>(connectionTimeout)
          , std::underlying_type_t<send_timeout>(sendTimeout)
          , std::underlying_type_t<auto_ack>(autoAck)};
}

create<ComplicatedToBuild,
         connection_timeout{2500},
         send_timeout{5}, 
         auto_ack{true}>();

DEMO


或者,您可以允许以任意顺序指定参数,而不是在参数/参数类型不匹配时引发错误:

#include <tuple>

template <typename T, auto Arg, auto... Args>
T create()
{
    auto t = std::make_tuple(Arg, Args...);
    return {
         std::underlying_type_t<connection_timeout>(std::get<connection_timeout>(t))
       , std::underlying_type_t<send_timeout>(std::get<send_timeout>(t))
       , std::underlying_type_t<auto_ack>(std::get<auto_ack>(t))
    };
}

template <typename T>
T create()
{
    return create<T, connection_timeout{5000}, send_timeout{10}, auto_ack{false}>();
}

create<ComplicatedToBuild,
         connection_timeout{2500},
         send_timeout{5},
         auto_ack{true}>();    

create<ComplicatedToBuild,
         auto_ack{true},
         send_timeout{5},
         connection_timeout{2500}>();

DEMO 2

答案 1 :(得分:3)

这是一种实现语法稍有不同的解决方案:

import MapView from "react-native-maps";

<MapView provider={MapView.PROVIDER_GOOGLE} style={styles.map} />

首先,我们需要一个create< connection_timeout<1>, send_timeout<2>, auto_ack<true> >(); 助手:

is_instantiation_of

然后,我们可以将“ strong typedef”定义为继承自template <typename T, template <auto...> class C> struct is_instantiation_of_impl : std::false_type { }; template <auto... Ts, template <auto...> class C> struct is_instantiation_of_impl<C<Ts...>, C> : std::true_type { }; template <typename T, template <auto...> class C> constexpr bool is_instantiation_of = is_instantiation_of_impl<T, C>::value; 的类:

std::integral_constant

最后,我们的界面将如下所示:

template <long long X>
struct connection_timeout : std::integral_constant<long long, X> { };

template <long long X>
struct send_timeout : std::integral_constant<long long, X> { };

template <bool X>
struct auto_ack : std::integral_constant<bool, X> { };

live example on godbolt.org


界面的变化更加戏剧化,代码可以更加简单:

template <typename ConnectionTimeout,
          typename SendTimeout,
          typename AutoAck>
auto create()
    -> std::enable_if_t<
        is_instantiation_of<ConnectionTimeout, connection_timeout> 
     && is_instantiation_of<SendTimeout, send_timeout>
     && is_instantiation_of<AutoAck, auto_ack>
    >
{
}