重载解析和运算符<<的问题用于模板类型 [英] problems with overload resolution and operator<< for templated types

查看:87
本文介绍了重载解析和运算符<<的问题用于模板类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个库,它应该对对象进行字符串化处理.

I'm writing a library and it should stringify objects.

我选择支持operator<<(ostream&....

另一件事是,我的库应该提供{?}形式中没有operator<<(ostream&...的类型的默认字符串化.

Another thing is that my library should provide default stringification of types that don't have operator<<(ostream&... in the {?} form.

问题出在诸如vector<>之类的模板类型上-我不希望用户为vector<int>vector<float>写2个重载-但我无法使其正常工作.

The problem is with templated types like vector<> - I don't want the user to write 2 overloads for vector<int> and vector<float> - but I cannot get it to work.

这是代码:

#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> // note the negation here compared to the one above
typename enable_if< ! has_insertion_operator<T>::value, string>::type stringify(const T&) {
    return "{?}";
}

// USER CODE:

struct myType {};

ostream& operator<<(ostream& s, const myType&) { s << "myType"; return s; }

template<typename T>
ostream& operator<<(ostream& s, const vector<T>&) { s << "vector<T>"; return s; }

int main() {
    myType a;           cout << stringify(a) << endl; // prints "myType"
                        cout << stringify(6) << endl; // prints "6"
    vector<int> v(5);   cout << stringify(v) << endl; // prints "{?}" instead of "vector<T>"

    return 0;
}

myType和整数都被字符串化,但是对于vector<int>我得到默认的{?}.

myType and the integer both get stringified but for the vector<int> I get the default {?}.

在这方面,我需要帮助-这对我来说是个绝妙的选择.我需要用户提供的operator<<(ostream&...重载即可直接使用而无需修改-而所有这些都在C ++ 98中实现.

I need help on this - it is a showstopper for me. I need user-provided operator<<(ostream&... overloads to work out-of-the-box without modification - and all this in C++98.

推荐答案

原来,我要做的就是通过插入操作符特征的实现删除名称空间,因此回退operator<<(ostream&...最终出现在全局范围内-解释此处.

Turns out all I had to do was remove the namespace with the implementation of the insertion operator trait so the fallback operator<<(ostream&... ends up in global scope - explained here.

这篇关于重载解析和运算符&lt;&lt;的问题用于模板类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆