Question

template <typename T, typename C>
class CSVWriter{
    template <typename PrinterT>
    void write(std::ostream& stream, const PrinterT& printer){

    }
};

I want to check whether there exists at least two overloads PrinterT::operator()(T*) and PrinterT::operator()(C*)
PrinterT may or may not inherit from std::unary_function
What concept Checking Classes I need to use here ?

(I am not using C++11)

Was it helpful?

Solution

You can use something like that

#include <iostream>

#include <boost/concept/requires.hpp>
#include <boost/concept/usage.hpp>

template <class Type, class Param>
class has_operator_round_brackets_with_parameter
{
public:
    BOOST_CONCEPT_USAGE(has_operator_round_brackets_with_parameter)
    {
        _t(_p);
    }

private:
    Type    _t;
    Param   _p;
};

struct X {};
struct Y {};

struct Test1
{
    void operator() (X*) const { }
};

struct Test2: public Test1
{
    void operator() (X*) const { }
    void operator() (Y*) const { }
};

template <class T, class C>
struct CSVWriter
{
    template <class PrinterT>
    BOOST_CONCEPT_REQUIRES(
        ((has_operator_round_brackets_with_parameter<PrinterT, T*>))
        ((has_operator_round_brackets_with_parameter<PrinterT, C*>)),
    (void)) write(std::ostream& stream, const PrinterT& printer)
    {

    }
};

int main()
{
    CSVWriter<X, Y> w;
    // w.write<Test1>(std::cout, Test1());  // FAIL
    w.write<Test2>(std::cout, Test2());     // OK
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top