STL容器的运算符< < 的C++泛型重载产生字符串的不明确重载错误

sg3maiej  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(79)

我的意思是写一个operator<<的泛型重载来打印STL容器。我把代码放在下面。每当operator<<涉及到string时,它就会产生一个编译错误ambiguous overload for 'operator<<',在 problem lines 1 and 2 的例子中。

**我怎样才能在不丢失泛型重载的情况下消 debugging 误,并且不必为每一种可能使用容器的字符串编写显式示例化?**也许从我的重载中排除了字符串。

#include <iostream>
#include <vector>
#include <set>
#include <list>
#include <map>
#include <tuple>
#include <string>

// Maximum number of printed values. After this, print "..."
#define MAX_PRINT_VALS 10

//=========================================================================
// Set of functions to dump STL containers

template <template <class...> class Container, class ...T>
std::ostream& operator<<(std::ostream& os, const Container<T...>& c)
{
    os << "[";

    size_t nvals = 0;
    for ( auto iter = c.begin() ; iter != c.end() ; iter++ ) {
        os << *iter;
        nvals++;
        if (iter != --c.end())
            os << ", ";
        if (nvals > MAX_PRINT_VALS) {
           os << "... (total of " << c.size() << " values)";
           break;
        }
    }

    os << "]";
    return os;
}

template<class Key, class T>
std::ostream& operator<<(std::ostream& os, const std::pair<Key, T>& p)
{
    os << "(" << p.first << ", " << p.second << ")";
    //os << std::endl;
    return os;
}

using namespace std;

int main(int argc, char **argv) {

    //============================================================
    // Print vector
    const size_t nmax = 3;
    vector<double const*> vec_dp;
    for (size_t n = 0; n < nmax; n++) {
        vec_dp.push_back(new double(n+1.5));
    }
    cout << "Vector of indices vec_dp = " << vec_dp << endl;
    for (size_t n = 0; n < nmax; n++) {
        delete vec_dp[n];
    }

    vector<string> vec_s;
    for (size_t n = 0; n < nmax; n++) {
        vec_s.push_back("asa");
    }
    cout << "Vector of string vec_s = " << vec_s << endl;         // PROBLEM LINE 1

    //============================================================
    // Print set
    set<double> set_d;
    for (size_t n = 0; n < nmax; n++) {
        set_d.insert(n+1.3);
    }
    cout << "Set of double set_d = " << set_d << endl;

    //============================================================
    // Print list
    list<double> list_d;
    for (size_t n = 0; n < (nmax + 10); n++) {
        list_d.emplace_back(n+1.4);
    }
    cout << "List of double list_d = " << list_d << endl;

    //============================================================
    // Print map
    typedef pair<int, int> pair2_t;
    map<pair2_t::first_type, pair2_t::second_type> map_i_i;
    for (size_t n = 0; n < (nmax + 10); n++) {
        map_i_i.insert(pair2_t(n+1, n+2));
    }
    cout << "Map of (int, int) map_i_i = " << map_i_i << endl;

    typedef pair<int, string> pair1_t;
    map<pair1_t::first_type, pair1_t::second_type> map_i_s;
    for (size_t n = 0; n < (nmax + 10); n++) {
        map_i_s.insert(pair1_t(n+1, "one"));
    }
    cout << "Map of (int, string) map_i_s = " << map_i_s << endl;         // PROBLEM LINE 2

    return 0;
}

相关

  1. C++ print template container error (error: ambiguous overload for 'operator<<') understanding?
  2. Ambiguous overload for ‘operator<<’ in ‘std::cout <<
  3. overloading << operator for c++ stl containers
  4. Narrowing down a C++ concept to exclude certain types
yqkkidmi

yqkkidmi1#

我怎样才能在不丢失泛型重载的情况下消除该错误,并且不必为带字符串的容器的每一种可能用途编写显式示例化?也许从我的重载中排除字符串
您可以使用std::enable_if(因为您使用的是c++17)为**std::string**排除您自己的重载,如下所示:

template <template <class... K> class Container, class ...T   >  
//added this to make use of SFINAE
std::enable_if_t<not (std::is_same_v<std::string, Container<T...>>),std::ostream&>                     operator<<(std::ostream& os, const Container<T...>& c)   
{                  
    //other code as before...

    os << "]";
    return os;
}
//other code as before...
int main(int argc, char **argv) {

     //other code as before...
     cout << "Vector of string vec_s = " << vec_s << endl; //works now    
}

Working demo
使用c++20,你可以使用requires,也许可以使代码更具可读性。Demo

相关问题