Exclude several types from an overload of operator<<

This is a follow-up of C++ generic overload of operator<< for STL containers produces ambiguous overload error with strings The answer works very well. But upon further testing, I found at least one class that already had its operator<< and also matched my overload, setfill.

My MCVE is posted below. Line #1 (see comment) produces compilation error ambiguous overload for 'operator<<' (operand types are 'std::ostringstream' {aka 'std::__cxx11::basic_ostringstream<char>'} and 'std::_Setfill<char>').

Is there any way to add multiple classes for excluding in std::enable_if_t<not (std::is_same_v<std::string, Container<T...>>), ... ? What is the specific syntax for my case, and a generic syntax to keep adding exclusions?

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

#include <sstream>
#include <iomanip>

template<typename T, typename... Ts>
struct contains : std::bool_constant<(std::is_same<T, Ts>{} || ...)>
{};
template <template <class... K> class Container, class ...T>
std::enable_if_t<not (std::is_same_v<std::string, Container<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;
}

using namespace std;

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

    //============================================================
    // Print iomanip flags
    ostringstream msg;
    int dT = 65205;
    msg << std::setfill('0')              // <--- Error Line #1
        << std::setw(2)
        << dT / 86400
        << "d";
    cout << "Elapsed time = " << msg << endl;

    return 0;
}

Realted

  1. Narrowing down a C++ concept to exclude certain types

Upvotes: 0

Views: 63

Answers (0)

Related Questions