我可以使用define调用运算符<<
Can I use a define to call the operator <<
我有一个带有以下重载的类:
template<typename T>
ParamContainer &operator<<(keyValue<T> &couple)
{
insert(couple.key_, couple.value_);
return *(this);
}
和keyValue
template <typename T>
struct keyValue
{
std::string key_;
T value_;
keyValue(std::string key, T &value): key_(key), value_(value){}
};
我想打电话给我的接线员<lt;像这样:
ParamContainer p;
p << ("value", "content") << ("id", 5);
所以我试图定义这个东西:
#define ParamContainer<<(X, Y) ParamContainer<<keyValue(X, Y)
或
#define ParamContainer::operator<<(X, Y) ParamContainer<<keyValue(X, Y)
但它没有编译:
src/TemplateEngine.hpp:48:25: warning: ISO C99 requires whitespace after the macro name [enabled by default] src/ControllerPost.cpp: In member function 'virtual void ControllerPost::operator()(boost::cmatch&, http::server3::reply&, boost::container::flat_map<std::basic_string<char>, std::shared_ptr<PostParam> >&)':
src/ControllerPost.cpp:32:19: error: expected unqualified-id before '<<' token
src/ControllerPost.cpp:32:19: error: 'X' was not declared in this scope
src/ControllerPost.cpp:32:19: note: suggested alternative:
/usr/local/include/boost/function/function_base.hpp:92:13: note: 'boost::detail::function::X'
src/ControllerPost.cpp:32:19: error: 'Y' was not declared in this scope
src/ControllerPost.cpp:32:19: error: expected ';' before 'ParamContainer'
src/ControllerPost.cpp:34:3: error: 'm' was not declared in this scope
来自第32行:
TemplateEngine::ParamContainer m;
m << ("name", "value");
如果我不能使用C++符号,我想我会找到另一种方法
函数样式和常量样式预处理器定义的名称都必须是有效的C++标识符。它们不能包含<<
符号,所以预处理器不可能实现您的技巧。
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 如何显式调用运算符<<
- 为什么COUT在朋友函数中不起作用,该功能超载了操作员&lt;&lt;这是一个iStream运算符
- C++运算符<<调用::ostream而不是std::osttream
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- 过载输出<<用于类的运算符,以打印其中的元组
- C++ostream:没有运算符匹配<<&应在'&'代币
- 重载运算符<<:此运算符函数的参数太多
- C++继承运算符<<
- 重载运算符<<用于模板类.即使使用好友关键字也无法获得私人会员
- 如何过载<<用于YAML::Emitter的运算符,以序列化包含另一个自定义类的向量的自定义类
- 为什么字符串流运算符<<擦除原始值
- 关于使用运算符<<为新手提供C++中的模板
- 我已经完成了<<运算符重载,但它'It’不起作用
- 重载运算符<<输出地址而不是数据成员
- 错误:没有匹配'运算符<<"在'std::cout
- 重载运算符<<用于ostream语法
- 当运算符<存在时,为什么要定义 LT?
- log4cxx访问异常,使用<<运算符和宏
- 重载<<运算符错误C2804:二进制'运算符<<'参数太多