使用 enable_if 专门化结构模板

Specializing struct template using enable_if

我正在尝试创建一个模板 class,它将实现具有不同签名的回调,具体取决于它是使用一种类型还是两种类型进行实例化。

struct NoIntermediate
{
};

template<typename R, typename I>
struct ParserCallbackSwitch
{
     using type = std::function<bool(const std::string &, R&, I&)>;
}

template<typename R, typename I = NoIntermediate>
class OtherClass
{
public:
    typedef ParserCallbackSwitch<R, I>::type ParserType;

}

现在我想添加代码,这样如果在实例化 'OtherClass' 时未指定 I,则 ParserCallbackSwitch 将是:

template<typename R, typename I>
struct ParserCallbackSwitch
{
      using type = std::function<bool(const std::string &, R&)>;
}

请注意,在这种情况下 ParserCallbackSwitch::type 是一个只有两个参数的函数。

我希望能够做到以下几点:

OtherClass<int, float> p; // p::ParserType = std::function<bool(std::string &, int &, float &);
OtherClass<int> q;        // q::ParserType = std::function<bool(std::string &, int &);

当 I 是 NoIntermediate 类型时(即未指定 I)

,我不知道如何部分指定 ParserCallbackSwitch

解决方案:基于以下回复。这是我最终使用的代码。

struct NoIntermediate {};

template<typename R, typename I = NoIntermediate>
struct ParserCallbackSwitch
{
    using type = std::function<bool(const std::string &, R&, I&)>;
};

template<typename R> 
struct ParserCallbackSwitch<R, NoIntermediate>
{
    using type = std::function<bool(const std::string &, R&)>;
};

template<typename R, typename I = NoIntermediate>
class OtherClass
{
   public:
   typedef ParserCallbackSwitch<R, I>::type ParserType;
}

所以!您没有正确地专门化模板。您正在定义两个不相关的 class 模板,它们恰好具有相同的名称。

有多种方法可以实现您的建议。这个给最不专业的模板一个参数包。

#include <functional>
#include <type_traits>

template<typename... S>
struct OtherClass;

template<typename R, typename I>
struct OtherClass<R, I> {
    using ParserType = std::function<bool(std::string&, R&, I&)>;
};

template<typename R>
struct OtherClass<R> {
    using ParserType = std::function<bool(std::string&, R&)>;
};

int main(void) {
    static_assert(std::is_same<OtherClass<int, float>::ParserType,
                            std::function<bool(std::string&, int&,
                                                float&)>>(),
                "Something wrong here.");
    static_assert(std::is_same<OtherClass<int>::ParserType,
                            std::function<bool(std::string&, int&)>>(),
                "Hmmmmmm.");
    return 0;
}

您在参数中使用默认类型的想法也可行,但您的语法略有偏差。这是它的样子。

#include <functional>
#include <type_traits>

template<typename R, typename I = void>
struct OtherClass {
    using ParserType = std::function<bool(std::string&, R&, I&)>;
};

template<typename R>
struct OtherClass<R, void> {
    using ParserType = std::function<bool(std::string&, R&)>;
};

int main(void) {
    static_assert(std::is_same<OtherClass<int, float>::ParserType,
                            std::function<bool(std::string&, int&,
                                                float&)>>(),
                "Something wrong here.");
    static_assert(std::is_same<OtherClass<int>::ParserType,
                            std::function<bool(std::string&, int&)>>(),
                "Hmmmmmm.");
    return 0;
}