强制操作符_(下划线)
Forcing operator_ (underscore)
这个问题是的乐趣,我知道我不能定义operator_
。
然而,我想真的想要"弯曲"这个规则,让下面的内容有效(有效是松散定义的!)
T result = somevar _ someother;
我没有一个可能的解决方案,但也许你可以,可能使用一些预处理器#define _ SOMETHING
是相当危险的)
任何帮助都非常感谢!
这个怎么样:
#include <iostream>
struct Underscore {};
#define _ | Underscore() |
template<typename T>
struct Has
{
const T& m_t;
Has( const T& t )
: m_t( t )
{
}
};
template<typename T>
Has<T> operator|( const T& lhs, const Underscore& )
{
return Has<T>( lhs );
}
template<typename T, typename U>
auto operator|( const Has<T>& lhs, const U& rhs )
{
std::cout << "This is the body of your iterator for " << lhs.m_t << " and " << rhs << std::endl;
return 0;
}
int main()
{
const std::string a = "a";
const std::string b = "b";
a _ b;
}
为了好玩,也许是这样的?
#include <iostream>
struct _magic_
{
};
template<class T>
struct enchanted
{
enchanted(T t) : value(t) {}
T value;
};
static constexpr auto _ = _magic_{};
template<class T>
enchanted<T> operator| (T&& t, _magic_ magic)
{
return {std::forward<T>(t)};
}
template<class T, class U>
auto operator| (const enchanted<T>& e, U u)
{
return e.value + u;
}
int main()
{
int x = 4;
int y = 5;
auto z = x |_| y;
std::cout << z << std::endl;
}
相关文章:
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 下划线不会与"发送输入"一起显示C++
- 在 QTextEdit C++ 中为特定行添加下划线
- 导出函数中有多少下划线('_')(C++
- 有没有选项不自动所有前导下划线 _ 以导出 emscripten 中的函数?
- 删除数字并在C++中保留字符串的下划线
- c++ 函数中额外的下划线名称
- 在C 中,Haskell下划线有其他选择吗?
- 使用与号后跟下划线命名的变量是什么意思?
- C++20 中的严格别名规则是否允许标准 c++ unicode 字符和下划线类型之间"reinterpret
- 用下划线填充空格
- 在Typedef结构中,下划线意味着什么
- 为什么非下划线名称保留给 UDL 的实现,而不是相反
- 下划线、名称和文字运算符
- 检查两个彼此相邻的两个下划线
- 使用查找检查,如果我的字符串中有下划线
- 代码的错误答案是在Java Camel案件和C 下划线标识符之间转换的错误答案
- 用户定义的文字、下划线和全局名称
- 如何在MFC C++中给文本加下划线
- 强制操作符_(下划线)