6

We use

x += y

instead of

x = x + y

And similarly for *,/,- and other operators. Well, what about

x min= y

instead of

x = std::min(x, y)

? Is there a commonly-used idiom for this command, not requiring the (impossible) extension of the language with another operator?

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 8
    Yes. `x min= y` is commonly referred to as a *syntax error*. :) – Moo-Juice Aug 05 '13 at 09:48
  • 1
    You can't invent new operators in C++ like that, so the idiom is what you've written: `x = std::min(x, y)`. – Some programmer dude Aug 05 '13 at 09:48
  • 2
    There isn't anything like that in c++ or any means to define it. – jcoder Aug 05 '13 at 09:48
  • 1
    `+=` is not two operators. It is one operator introduced in the language for short notation, because it's so commonly used operation. Introducing an operator for all possible functions seems unfeasible. – lapk Aug 05 '13 at 09:49
  • @Moo-Juice Unless `min` is a macro. (But it better not be, because if it is, we can't include any standard headers.) – James Kanze Aug 05 '13 at 09:57

7 Answers7

21

It's certainly not idiomatic, but you might be able to use something called named operators (see these Q&As here and here, developed by @Yakk and @KonradRudolph), and write

x <min>= y;

which is made possible by overloading operator< and operator>, combined with a clever wrapped named_operator. The full code is given by the link above, but uses code like

template <typename T1, typename T2, typename F>
inline auto operator >(named_operator_lhs<T1, F> const& lhs, T2 const& rhs)
    -> decltype(lhs.f(std::declval<T1>(), std::declval<T2>()))
{
    return lhs.f(lhs.value, rhs);
}

Using std::min as template argument for the template parameter F, would update the lhs of the expression with the min of the lhs and rhs.

Community
  • 1
  • 1
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
2

You can't extend the language in this way. The closest you can come is something like:

template <typename T, typename U>
T&
mineq( T& lhs, U rhs )
{
    if ( rhs < lhs ) {
        lhs = rhs;
    }
    return lhs;
}

This would allow writing:

mineq( x, y );

I question whether it's worth the bother, however.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
1

NO. There is no such thing, you'll have to do with std::min(x,y);

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
1

No, it is not possible to create new custom operators.

You have a few available solutions though:

llama_min_age = std::min(x, y);
llama_min_age = (x < y ? x : y);

Or even a macro if you want to:

#define MIN(x, y) ((x) < (y) ? (x) : (y))

About the macro: it can lead to vicious bug, so I would prefer to use one of the first two solutions.

Xaqq
  • 4,308
  • 2
  • 25
  • 38
0

You cannot write this kind of sentences, they are reserved for the built-in syntaxt

Miguel Prz
  • 13,718
  • 29
  • 42
0

The options you have:

x = std::min(x,y)

or

x = y < x ? y : x;

or

if (y < x) x = y;
piokuc
  • 25,594
  • 11
  • 72
  • 102
0

C++ has a limited set of operators and keywords.

What you are trying to do is outside the C++ specification and is not possible.

You can do the comparison and assignment with this one-liner if you want:

x = (x < y) ? x : y;

Dariusz
  • 21,561
  • 9
  • 74
  • 114
  • Only six? I count eight. –  Aug 05 '13 at 13:24
  • @James `x = (x SPACE1 < SPACE2 y) SPACE3 ? SPACE4 x SPACE5 : SPACE6 y;` –  Aug 05 '13 at 13:27
  • @H2CO3 this is so trivial an example that if you know the syntax you'll know what it does immediately. If it was used somewhere else, in a long line of code, complex call, I'd understand your comments. But in this case... Your're more pedantic than `gcc -pedantic`. But OK, I'll edit the post, just for the two of you. – Dariusz Aug 05 '13 at 13:29
  • @James and yes, I think that 6 spaces is better than 8 in this case. – Dariusz Aug 05 '13 at 13:30
  • @Dariusz Thanks. It's not that it's not obvious - of course I grasp what it means. It's just that it really is more readable with spaces. And yeah, I'm usually more pedantic than `cc -pedantic` :) –  Aug 05 '13 at 13:30
  • @H2CO3 Ah well, you say `tomato`, I say `potato`. ;) Personally, I put spaces inside braces/parentheses/brackets, except when there are multiple together in which case I collapse them (except for the inner pair if they're empty). For example, `fn( fn2( param, fn3() ))`. –  Aug 05 '13 at 13:48
  • @James Yeah, I know, and it's ugly :) I follow the Linux Kernel coding style - the best style I know of. –  Aug 05 '13 at 13:50
  • @H2CO3 The Linux Kernel style is very similar to what I have to use at work now. Suffice to say I have not had to work with such unreadable code in a very long time. In many respects it reminds me of how I used to program when I was first learning C over 20 years ago. But anyway, I think we're sufficiently off topic now. :) –  Aug 05 '13 at 13:55