模板类型的重载解析和运算符<<问题 - 第 2 部分

problems with overload resolution and operator<< for templated types - part 2

本文关键字:lt 问题 部分 运算符 类型 重载      更新时间:2023-10-16

给定以下代码:

#include <string>
#include <type_traits>
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
namespace has_insertion_operator_impl {
    typedef char no;
    typedef char yes[2];
    struct any_t {
        template <typename T>
        any_t(T const&);
    };
    no operator<<(ostream const&, any_t const&);
    yes& test(ostream&);
    no   test(no);
    template <typename T>
    struct has_insertion_operator {
        static ostream& s;
        static T const&      t;
        static bool const    value = sizeof(test(s << t)) == sizeof(yes);
    };
}
template <typename T>
struct has_insertion_operator : has_insertion_operator_impl::has_insertion_operator<T> {};
template <class T>
typename enable_if<has_insertion_operator<T>::value, string>::type stringify(const T& in) {
    stringstream stream;
    stream << in;
    return stream.str();
}
template <class T>
typename enable_if< ! has_insertion_operator<T>::value, string>::type stringify(const T&) {
    return "{?}";
}
// ======= OVERLOADS PROVIDED BY THE USER =======
template<typename T, typename T2>
struct myType { T data; T2 op; };
template<typename T, typename T2>
ostream& operator<<(ostream& s, const myType<T, T2>&) { s << "myType"; return s; }
template<typename T>
ostream& operator<<(ostream& s, const vector<T>&) { s << "vector<T>"; return s; }
template<typename T, typename A>
ostream& operator<<(ostream& s, const vector<T, A>&) { s << "vector<T, A>"; return s; }
int main() {
    myType<int, float> a;   cout << stringify(a) << endl; // prints "myType"
                            cout << stringify(6) << endl; // prints "6"
    vector<int> v(5);       cout << stringify(v) << endl; // prints "{?}"
    return 0;
}

为什么模板myType<>被字符串化,而模板化的vector<>类型没有?

对于vector<>类型,我得到默认的{?}字符串化,但我显然希望调用底部的重载之一,就像myType<> 一样

编辑:

这里的实际问题是为什么has_insertion_operator<vector<int>>是假的

我在C++98 中也需要这个

并且operator<<过载应该在stringify()之后提供,就像myType<> 一样

我认为问题出在查找上,我会提供我的理解。

当您在has...结构参数相关查找中调用operator<<时,由于myType与重载的operator<<位于同一命名空间中,因此它将被创建,并且您将获得正确的字符串。但是,当您尝试输出vector时,它会尝试通过相同的参数相关查找规则来搜索重载的operator<<,但由于std命名空间中没有重载运算符,因此无法执行此操作。因此,它回到了从进行调用的命名空间开始的非限定搜索,因此它找到了您的存根operator<<

因此,为了修复它,您可能在std命名空间中放置了operator<<重载(这是标准所禁止的),或者删除了您的命名空间——这将产生相同的效果。

不过,您不必将所有内容都放在私有命名空间之外。在全局名称空间中做这样的事情就足够了:

typedef char no;
typedef char yes[2];
template<typename T>
no operator<<(ostream const&, T const&);

或者,如果可能的话,最好要求库用户将其重载放入其类所在的相同命名空间中。不过,它不适用于std成员。

这里有两个问题。

首先是has_insertion_operator<>谓词中的某些内容出错。

我把它换成了这个。。。

template<class T>
struct has_insertion_operator
{
    template<class U>
    static auto test(U*p) -> decltype((std::declval<std::ostream>() << (*p)), void(), std::true_type());
    template<class U>
    static auto test(...) -> std::false_type;
    static constexpr bool value = decltype(test<T>(0))::value;
};

它解决了这个问题,并强调了下一个问题(对你来说可能更严重):

./stringify.cpp:73:12: error: call to function 'operator<<' that is neither visible in the template definition nor found by argument-dependent lookup
    stream << in;
           ^
./stringify.cpp:100:37: note: in instantiation of function template specialization 'stringify<std::__1::vector<int, std::__1::allocator<int> > >' requested here
    vector<int> v(5);       cout << stringify(v) << endl; // prints "{?}"
                                    ^
./stringify.cpp:91:10: note: 'operator<<' should be declared prior to the call site
ostream& operator<<(ostream& s, const vector<T>&) { s << "vector<T>"; return s; }
         ^
1 error generated.

这是因为模板函数operator<< <std::vector...>在定义之前就被引用了。

一旦你把它的定义提升到字符串的定义之上,一切都会起作用。

最后,注:

operator<<重载为std::vector是一个非常糟糕的主意。以后它会让你头疼。

但我使用的是c++98,我希望用户能够提供自己的过载[专业化]

好吧,那么,让我们用一种简单(更正确)的方式来做吧,它适用于所有风格的c++,并且不会因为非法重载入侵std命名空间而引起头痛:

#include <string>
#include <sstream>
#include <vector>
#include <iostream>

// define a template class emitter which by default simply calls operator<<
template<class T>
struct emitter
{
    emitter(const T& v) : _v(v) {}
    std::ostream& operator()(std::ostream& os) const {
        return os << _v;
    }
    const T& _v;
};
// emitter<>'s are streamable
template<class T>
std::ostream& operator<<(std::ostream& os, const emitter<T>& e)
{
    return e(os);
}
// a factory function to generate the correct emitter
template<class T>
emitter<T> emit(const T& v)
{
    return emitter<T>(v);
}
// write one version of stringify in terms of emit<>()
template <class T>
std::string stringify(const T& in) {
    std::stringstream stream;
    stream << emit(in);
    return stream.str();
}
// ======= OVERLOADS PROVIDED BY THE USER =======
template<typename T, typename T2>
struct myType { T data; T2 op; };
// user must provide an emitter for each custom type
template<typename T, typename T2>
struct emitter<myType<T, T2> >
{
    typedef myType<T, T2> value_type;
    emitter(const value_type& v) : _v(v) {}
    std::ostream& operator()(std::ostream& os) const
    {
        return os << "myType";
    }
private:
    const value_type& _v;
};
// and for any std:: templates he wants to support
template<class V, class A>
struct emitter<std::vector<V, A> >
{
    typedef std::vector<V, A> value_type;
    emitter(const value_type& v) : _v(v) {}
    std::ostream& operator()(std::ostream& os) const
    {
        return os << "vector<T, A>";
    }
private:
    const value_type& _v;
};
// test
int main() {
    myType<int, float> a;  std::cout << stringify(a) << std::endl; // prints "myType"
                           std::cout << stringify(6) << std::endl; // prints "6"
    std::vector<int> v(5); std::cout << stringify(v) << std::endl; // prints "vector<T, A>"
    return 0;
}