c++ - 仅为特定模板类启用模板

标签 c++ c++11 variadic-templates static-assert enable-if

这个问题的灵感来自于我之前的问题No template parameter deduction of parameter pack .

考虑以下代码示例:

#include <memory>
#include <string>

template<typename... FArgs>
class Callback
{
    public:
    class Handle{};
};

class BaseCallbackHandle
{
};

using TypeErasedCallbackHandle = std::unique_ptr<BaseCallbackHandle>;

template<typename H>
TypeErasedCallbackHandle makeTypeErasedCallbackHandle( H handle)
{
   return {};
}

int main()
{
    Callback<int>::Handle h;
    std::string s;
    makeTypeErasedCallbackHandle(h); //should compile fine
    makeTypeErasedCallbackHandle(s); //should raise a compile error
}

另见 http://coliru.stacked-crooked.com/a/5f2a2e816eef6afd

函数模板makeTypeErasedCallbackHandle现在将任何类作为输入参数。有什么方法可以确保(例如使用 static-assert 或 enable_if),只有 Callback<FArgs...>::Handle (与任何 FArgs )被允许作为 HCallback<int>::Handle 的示例应编译,同时 std::string将失败。

最佳答案

Handle 类中定义一个类型,并在 makeTypeErasedCallbackHandle() 中引用该类型:

#include <memory>
#include <string>

template <typename... FArgs>
struct Callback {
    struct Handle {
        using callback_type = Callback<FArgs...>;    
    };
};

struct BaseCallbackHandle {
};

using TypeErasedCallbackHandle = std::unique_ptr<BaseCallbackHandle>;

template <typename H>
TypeErasedCallbackHandle makeTypeErasedCallbackHandle(H handle) {
    using callback_type = typename H::callback_type;

    return {};
}

int main() {
    Callback<int>::Handle h;
    std::string s;
    makeTypeErasedCallbackHandle(h); //should compile fine
    makeTypeErasedCallbackHandle(s); //should raise a compile error
}

Live example

对于任何未定义嵌套类型的 H,这将在实例化期间失败。


稍加努力,您就可以static_assert 向客户端生成有意义的消息,同时通过类型特征增加解决方案的灵 active 。这样做的好处是 callback_impl::is_callback 可以专用于任意句柄类型:

#include <memory>
#include <string>

namespace callback_impl {

struct callback_identification_type {};

template <typename T, typename = void>
struct is_callback : std::false_type {};

template <typename T>
struct is_callback<T,
    std::enable_if_t<std::is_same<typename T::callback_id_type,
                     callback_identification_type>::value>>
 : std::true_type {};

}

template <typename... FArgs>
struct Callback {
    struct Handle {
        using callback_id_type = callback_impl::callback_identification_type;    
    };
};

struct BaseCallbackHandle {
};

using TypeErasedCallbackHandle = std::unique_ptr<BaseCallbackHandle>;

template <typename H>
TypeErasedCallbackHandle makeTypeErasedCallbackHandle(H handle) {
    static_assert(callback_impl::is_callback<H>::value,
                  "The handle type is not a member of a recognised Callback<T...>");
    return {};
}

int main() {
    Callback<int>::Handle h;
    std::string s;
    makeTypeErasedCallbackHandle(h); //should compile fine
    makeTypeErasedCallbackHandle(s); //should raise a compile error

    return 0;
}

Live example

输出:

g++ -std=c++14 -O2 -Wall -Wno-unused-local-typedefs -pedantic -pthread main.cpp && ./a.out
main.cpp: In instantiation of 'TypeErasedCallbackHandle makeTypeErasedCallbackHandle(H) [with H = std::__cxx11::basic_string<char>; TypeErasedCallbackHandle = std::unique_ptr<BaseCallbackHandle>]':
main.cpp:41:35:   required from here
main.cpp:32:5: error: static assertion failed: The handle type is not a member of a recognised Callback<T...>
     static_assert(callback_impl::is_callback<H>::value,
     ^~~~~~~~~~~~~

关于c++ - 仅为特定模板类启用模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37874287/

相关文章:

c++ - 避免使用模板函数的 if-else 语句

c++ - 虚函数在哪里使用 vpointers to vtables 来解析方法调用,非虚方法存储在哪里以及它们是如何解析的?

c++ - 带有模板成员函数和 std::invocable 的 C++20 概念中的错误

C++ 类继承 : Functions

c++ - 给定 std::tuple<T...> 时可变参数模板类的实例化?

c++ - 使用可变参数模板打印列表

c++ - 为什么忽略返回类型 'cv'?

c++ - 无法访问嵌套私有(private)枚举的枚举数

c++ - 在此类中的函数中使用另一个类中的函数或结构

c++ - 需要有关在 gcc-7.2.0 中有编译错误但在 gcc-6.4.0 中没有的代码的帮助