C++ boost概念检查运算符()重载 模板 类CSV编写器{ 样板 无效写入(标准::ostream&stream、const PrinterT&printer){ } };
我想检查是否至少存在两个重载C++ boost概念检查运算符()重载 模板 类CSV编写器{ 样板 无效写入(标准::ostream&stream、const PrinterT&printer){ } };,c++,boost,c++-concepts,C++,Boost,C++ Concepts,我想检查是否至少存在两个重载PrinterT::operator()(T*)和PrinterT::operator()(C*) PrinterT可以从std::一元函数继承,也可以不继承 我需要在这里使用什么概念检查类 (我没有使用C++11)你可以使用类似的东西 template <typename T, typename C> class CSVWriter{ template <typename PrinterT> void write(std::o
PrinterT::operator()(T*)
和PrinterT::operator()(C*)
PrinterT
可以从std::一元函数继承,也可以不继承
我需要在这里使用什么概念检查类
(我没有使用C++11)你可以使用类似的东西
template <typename T, typename C>
class CSVWriter{
template <typename PrinterT>
void write(std::ostream& stream, const PrinterT& printer){
}
};
#包括
#包括
#包括
样板
类具有带参数的\u运算符\u圆括号\u
{
公众:
BOOST\u概念\u用法(具有\u运算符\u圆括号\u和\u参数)
{
_t(p);
}
私人:
类型(t),;
参数p;
};
结构X{};
结构Y{};
结构测试1
{
void运算符()(X*)常量{}
};
结构Test2:公共Test1
{
void运算符()(X*)常量{}
void运算符()(Y*)常量{}
};
样板
结构CSVWriter
{
样板
增强概念要求(
((有_运算符_圆括号_和_参数))
((具有带参数的_运算符_圆括号_)),
(无效)写入(标准::ostream&stream、常量PrinterT&打印机)
{
}
};
int main()
{
CSVWriter w;
//w.write(std::cout,Test1());//失败
w、 写入(std::cout,Test2());//确定
返回0;
}
难道我不能使用像std::一元函数
或已经在boost
中的函数来代替编写我自己的的吗?我还得到了编译错误`错误:预期的构造函数、析构函数或之前的类型转换'('token`@NeelBasu:@NeelBasu:1.51和1.42版本在boost wiki上没有区别,所以应该是1.gcc版本4.4.5(Ubuntu/Linaro 4.4.4-14ubuntu5.1)
#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;
}