提升概念检查运算符()重载

boost concept check operator() overload

本文关键字:重载 运算符 检查      更新时间:2023-10-16
template <typename T, typename C>
class CSVWriter{
    template <typename PrinterT>
    void write(std::ostream& stream, const PrinterT& printer){
    }
};

我想检查是否存在至少两个重载PrinterT::operator()(T*)PrinterT::operator()(C*)
PrinterT可能继承也可能不继承自 std::unary_function
我需要在这里使用什么概念检查类?

(我没有使用 C++11)

你可以使用类似的东西

#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;
}