是否可以检查运算符是否重载<< for type or class exists?

问题描述 投票:0回答:1

我正在尝试检查运算符是否重载<< exists at compile time using c++17. Ideally it is supposed to be something like following:

template<typename T>
static void serialize_storage(const void* this_, std::ostream& stream) {
    if constexpr (std::is_invocable<...>::value) {
        stream << (*reinterpret_cast<const T*>(this_));
    } else {
        throw std::runtime_error("Type can not be serialized");
    }
}

将 is_invocable 的参数形成为运算符似乎很棘手<< is overloaded as member of std::ostream or just as "standalone" operator. So I tried two different functions first.

示例:

#include <map>
#include <string>
#include <iostream>

using Mmap = std::map<std::string, std::string>;

std::ostream& operator<<(std::ostream& stream, const Mmap& map) {
    stream << map.size();
    return stream;
}

template<typename T>
typename std::enable_if<std::is_invocable<decltype (operator <<(std::declval<std::ostream&>(), std::declval<const T&>())), std::ostream&, const T&>::value, void>::type
serialize_storage(const void* this_, std::ostream& stream) {
    stream << (*reinterpret_cast<const T*>(this_));
}

template<typename T>
typename std::enable_if<std::is_invocable<decltype (std::declval<std::ostream>().operator <<(std::declval<T>())), std::ostream, T>::value, void>::type
serialize_storage(const void* this_, std::ostream& stream) {
    stream << (*reinterpret_cast<const T*>(this_));
}

int main(int , char* [])
{

    Mmap foo;
    char boo = 'A';

    serialize_storage<Mmap>(&foo, std::cerr);
    serialize_storage<char>(&boo, std::cerr);
    std::cerr << std::endl;

    return 0;
}

但是编译器不能替换这两种类型。它看到了候选者,但没有看到 std::ostream::operator<<(char) nor overloaded std::ostream& operator<<(std::ostream& stream, const Mmap& map) fit is_invocable condition.

c++17 sfinae
1个回答
1
投票

你可以像这样添加特质(

is_streamable
):

#include <type_traits>

// A helper trait to check if the type supports streaming
template<class T>
class is_streamable {

    // match if streaming is supported
    template<class TT>
    static auto test(int) ->
        decltype( std::declval<std::ostream&>() << std::declval<TT>(), std::true_type() );

    // match if streaming is not supported:
    template<class>
    static auto test(...) -> std::false_type;

public:
    // check return value from the matching "test" overload:
    static constexpr bool value = decltype(test<T>(0))::value;
};

template<class T>
inline constexpr bool is_streamable_v = is_streamable<T>::value;

然后你可以像这样使用它:

template<class T>
static void serialize_storage(const void* this_, std::ostream& stream) {
    if constexpr (is_streamable_v<T>) {
        stream << (*reinterpret_cast<const T*>(this_));
    } else {
        throw std::runtime_error("Type can not be serialized");
    }
}

或者你可以将它用作类型特征:

template <class T, typename = std::enable_if_t<is_streamable<T>::value>>
static void serialize_storage(const void* this_, std::ostream& stream) {
    stream << (*reinterpret_cast<const T*>(this_));
}

演示

© www.soinside.com 2019 - 2024. All rights reserved.