gpt4 book ai didi

c++ - 我可以使用定义来调用运算符 <<

转载 作者:行者123 更新时间:2023-11-28 07:32:17 24 4
gpt4 key购买 nike

我有一个具有以下重载的类:

template<typename T>
ParamContainer &operator<<(keyValue<T> &couple)
{
insert(couple.key_, couple.value_);
return *(this);
}

和键值

  template <typename T>
struct keyValue
{
std::string key_;
T value_;
keyValue(std::string key, T &value): key_(key), value_(value){}
};

我想这样称呼我的接线员<<:

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++ 标识符。它们不能包含 <<符号,所以你的把戏不可能用预处理器来做。

关于c++ - 我可以使用定义来调用运算符 <<,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17427811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com