C++ 强制运算符(下划线)

C++ 强制运算符(下划线),c++,c++11,operator-overloading,c++14,C++,C++11,Operator Overloading,C++14,这个问题是为了好玩,我知道我不能定义操作符 然而,我想真的想要“弯曲”这个规则,让下面的内容成为有效的(valid的定义是松散的!) 我没有一个可能的解决方案,但也许你可以,也许可以使用一些预处理器。(当然,仅仅定义一些东西是相当危险的) 非常感谢您的帮助 也许是为了好玩,像这样的事 #include <iostream> struct _magic_ { }; template<class T> struct enchanted { enchant

这个问题是为了好玩,我知道我不能定义
操作符

然而,我想真的想要“弯曲”这个规则,让下面的内容成为有效的(valid的定义是松散的!)

我没有一个可能的解决方案,但也许你可以,也许可以使用一些预处理器。(当然,仅仅定义一些东西是相当危险的)


非常感谢您的帮助

也许是为了好玩,像这样的事

#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;
}
#包括
结构魔法_
{
};
模板
结界
{
附魔(T):值(T){}
T值;
};
静态constexpr auto=\u magic{};
模板
附魔运算符|(T&&T,_magic_magic)
{
返回{std::forward(t)};
}
模板
自动操作员|(const enchanted&e,U)
{
返回e.value+u;
}
int main()
{
int x=4;
int y=5;
自动z=x | | y;
std::cout这个怎么样:

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

}
#包括
结构下划线{};
#定义124;下划线()|
模板
结构有
{
康斯特科技大学;
民政事务总署(康斯特电讯)
:m_t(t)
{
}
};
模板
Has运算符|(常量T&lhs,常量下划线&)
{
返回有(lhs);
}
模板
自动操作员|(常数有左、左、常数U和右)
{

出于兴趣,你想要这个“接线员”做什么要做什么?您已经给出了答案,只需使用您选择的
#define(定义)OPERATOR(运算符)
),但它的乐趣是什么呢?我想,宏和模糊代码都不是fun@RichardHodges任何东西,返回另一个具有不同内容的
T
,我现在没有具体的想法
@tobi303如果可能的话,乐趣在于尽量避免
定义
。是的,我发现模糊处理很有趣!:@nwp好吧,有趣与否仍然是一个公开的争论……那么定义自定义类型的魔术呢?@senseiwa只是提供了
模板自动操作符的专业化(const-enchanted&e,U)
#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;

}