-6

This question is for fun, I know that I cannot define operator_.

However, I'd really like to "bend" this rule, having something like the following as valid (with valid being loosely defined!).

T result = somevar _ someother;

I didn't come with a possible solution, but maybe you can, possibly using some preprocessor übertricks. (Of course just #define _ SOMETHING is quite a bit dangerous)

Any help is greatly appreciated!

senseiwa
  • 2,369
  • 3
  • 24
  • 47
  • Out of interest, what would you like this "operator" to do? – Richard Hodges Jun 30 '16 at 09:01
  • 1
    You already give the answer, simply use `#define _ OPERATOR_OF_YOUR_CHOICE`, but what is the fun about it? Imho neither macros nor obfuscating code is fun – 463035818_is_not_an_ai Jun 30 '16 at 09:02
  • @RichardHodges anything, returning another `T` with different content, I have nothing specific in mind now. Take for instance `class T { public: int content; };`. – senseiwa Jun 30 '16 at 09:04
  • @tobi303 The fun is trying to avoid `#define`, if possible. And yes, I find that obfuscating is funny, though! :) – senseiwa Jun 30 '16 at 09:05
  • what types do `somevar` and `someother` have? – Ferenc Deak Jun 30 '16 at 09:05
  • @fritzone For simplicity, let's say they are all homogeneous in `T`, as in `class T { public: int content; };`. – senseiwa Jun 30 '16 at 09:06
  • well if it is about this kind of fun, then here is my answer: Use a `-` instead of a `_` and just dont look so close at it, they almost look the same and anyhow you can overload operators as you wish – 463035818_is_not_an_ai Jun 30 '16 at 09:08
  • 1
    @nwp Well, having fun or not is still an open debate... http://stackoverflow.com/questions/5508110/why-is-this-program-erroneously-rejected-by-three-c-compilers – senseiwa Jun 30 '16 at 09:12
  • Didn't you know that people hate [fun](http://www.stroustrup.com/whitespace98.pdf) on SO? – nwp Jun 30 '16 at 09:14
  • @nwp this is serioulsy by Bjarne? I cant believe it – 463035818_is_not_an_ai Jun 30 '16 at 09:16
  • Hmm..., how would be this parsed `int some_var; some_war++;`? Since underscores can be part of identifiers. – PcAF Jun 30 '16 at 09:28
  • OP basically asks for user-defined literals functioning as operators. It's a perfectly valid question and a feature available in many languages (incl. C++), hence I hardly get the downvotes.. why? – lorro Jun 30 '16 at 09:39
  • @tobi303 : Look at its date and read the last paragraph. – ildjarn Jun 30 '16 at 09:55
  • @lorro : The question has nothing to do with user-defined literals – that's the problem. – ildjarn Jun 30 '16 at 09:56
  • @ildjarn But I never said anything about user-defined literals, I asked about operators (as far as I see). – senseiwa Jun 30 '16 at 10:36
  • @senseiwa : That's what I said. o_O – ildjarn Jun 30 '16 at 10:37
  • @ildjarn: While I understand that the wording could be better (an edit perhaps possible), the question as I see is the same as I've rephrased it. – lorro Jun 30 '16 at 10:45
  • @ildjarn Sorry, I misunderstood! – senseiwa Jun 30 '16 at 10:48
  • Note that this is a pretty bad idea. `_` is usually used in the GNU gettext system to mark a string as translatable, so it is *already* defined as a macro which invokes a call to gettext in such programs. If you don't follow this convention then you would have to customize the `xgettext` program which extracts the dictionary of translatable strings from your program's source. So most internationalized programs wouldn't be able to use code which makes use of a custom `operator _` hack, or of libraries that use such a hack in their headers. – Chris Beck Jun 30 '16 at 22:18
  • Why do you want to do this? What programming purpose does it serve? If you can't explain that then it's not clear what "bending the rules" means -- how far can an answer bend the rules before not being useful. – Chris Beck Jun 30 '16 at 22:22
  • It's just something I'd like to have, just because there is no `operator_`. So, I'd like to have that, with no implementation in mind, could be for instance concatenation of strings, if you want some target. – senseiwa Jul 01 '16 at 07:33

2 Answers2

1

For fun, something like this, maybe?

#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;
}
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
1

How about this:

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

}
lorro
  • 10,687
  • 23
  • 36