This question already has answers here:
What are the basic rules and idioms for operator overloading?
(8 answers)
Closed 7 years ago.
Is it possible to create my own operators like '+' or '/'
Is it possible in C++?
I have already found operator '#' , but I do not know how to do this with another.
You can overload +, - for a given class, so they have custom behavior.
You can define preprocessor macros like #define OP(a,b) ((a))-(b)*(a)) and write code like 3 OP 4.
Other than that, I don't believe there's anything deep you can do in C++ to create your own new tokens or syntax. You can always write your own programming language - many people do - and creating a new operator pretty much means you're writing a new programming language by itself.
With the trivial Google search I found a table identifying 42 C++ operators that can be overloaded, and 4 C++ operators that cannot.
I did not verify this information.
If by create, you mean to define,
You can overload many defult operators for custon types, In fact for all of:
+ - * / % ˆ & | ~ ! = < > += -= *= /= %= ˆ= &= |= << >> >>= <<= == != <= >= && || ++ -- , ->* -> ( ) [ ]
Do do custom things for your classes.
You can also define functions, like float add(...) and define that to do whatever you like
Related
One of the uses of ... is to denote variadic entities in C and C++.
What is its name?
Is it classified as an operator or something else when used that way?
Any other details regarding ...?
Edit: I know the purpose of .... I am asking about its name and classification, which I hope, is similar in C and C++.
It is one of the punctuators.
6.4.6 Punctuators
Syntax punctuator:
one of [ ] ( ) { } . ->
++ -- & * + - ~ !
/ % << >> < > <= >= == != ^ | && ||
? : ; ...
= *= /= %= += -= <<= >>= &= ^= |=
, # ##
<: :> <% %> %: %:%:
In the function declaration it is called the ellipsis.
Ellipsis is also used by some compiler C language extensions.
Example - gcc switch/case range extension
const char *test(unsigned num)
{
switch(num)
{
case 0 ... 9:
return "the value is in the 0 to 9 range";
case 10 ... 99:
return "the value is in the 10 to 99 range";
default:
return "out of tested range";
}
}
https://godbolt.org/z/YBLma-
The ... is referred to as an ellipsis both in English and in the C standard.
One of the uses of ... is to denote variadic entities in C and C++.`
Yes, In layman's terms ... can be thought of as denoting more than one or multiples (as in pseudo-code punctuation we sometimes use multiple dots to resemble different types) of a use case, for which if we consider variadics (being multiple in the sense of 'varying' arguments/parameters) in C++, it would refer to a variable number of arguments for functions or templates.
What is its name?
Ellipsis
Is it classified as an operator or something else when used in that way?
No, it's definitely not an operator as it allows you to pass any number of arguments, not operate on them.
Any other details regarding ...?
As far as I know -
Its a special specifier;
The ellipsis always comes last in the argument list;
As far as its usage is concerned, its only used when you want to remove the limits on the number of parameters for a template/function or when you require to have an extensible number of parameters for expansion. (i.e. it provides parameter pack expansion in a variadic class template or function template) In practice we mostly require a fixed set of known parameters, so it isn't applicable to most cases;
It can be used with sizeof operator, as it's classified as a pack expansion as well.
Edit: I know the purpose of ... I am asking about its name and classification, which I hope, is similar in both C and C++.
The name is same, but usage may vary for C++ and C.
Am only familiar with its use in the former language. (I remember having a HackerRank problem on Variadics, covering its utility.)
The sequence of three full stops ... is called an ellipsis in both C and C++
In C++, the ellipsis helps initialize and expand different kinds of packs.
A parameter pack - when there is an ellipsis between the type and the identifier
Type ... identifier
A pack expansion - consists of a pattern and an ellipsis
pattern...
Here is a very simple C++ application I made with QtCreator :
int main(int argc, char *argv[])
{
int a = 1;
int b = 2;
if (a < 1 or b > 3)
{
return 1;
}
return 0;
}
To me, this is not valid C++, as the keyword or is not a reserved keyword.
But if I compile and run it, it works fine without any warnings ! The exit code is 0 and if I change b = 4, the exit code is 1 !
I'm not including anything to make sure there is no hidden define.
This is really strange to me. Is this something Qt is defining ? I didn't find anything in the documentation regarding that.
According to Wikipedia:
C++ defines keywords to act as aliases
for a number of symbols that function
as operators: and (&&), bitand (&),
and_eq (&=), or (||), bitor (|), or_eq
(|=), xor (^), xor_eq (^=), not (!),
not_eq (!=), compl (~).
As MadKeithV points out, these replacements came from C's iso646.h, and were included in ISO C++ as operator keywords. The Wikipedia article for iso646.h says that the reason for these keywords was indeed for international and other non-QWERTY keyboards that might not have had easy access to the symbols.
or is a C++ keyword, and you're allowed to use it instead of ||. There is no magic.
The same goes for and and most other logical operators. It's generally best to stick to the commonly known names though, to avoid confusion like this. If you use or, someone will wonder "why does this compile" ;)
iso646.h defines a number of operator alternatives - it's part of the C++ standard.
This question already has answers here:
Why is there no ^^ operator in C/C++?
(7 answers)
Logical XOR operator in C++?
(11 answers)
Closed 6 years ago.
Is there any tricky way to use logical xor operator ^^ in macro in C, C++, Objective-C?
I have tried applying ^^ directly in Objective-C, it does not work.
Edited: let me clarify my answer.
What I want is to use xor operator in macro. It does not mean "how to define the xor operator by a macro.
I.e, I want something like
#if defined(x) ^^ TARGET_OS_IOS ^^ __cplusplus
For seconds after posting the question, I figured out the answer my self.
!(A) != !(B) will be equivalent to xor operator
A better solution in case the number of operands is different than 2
!(A) ^ !(B) ^ !(C) ^ ...
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
In C, we could use ternary like
( a == 4) ? a = b: a = 5;
Someone told me better not using it, how do you think?
The conditional operators is useful for forming expressions. Use it when you need an expression which depends, well, on a condition. Don't use it to make statements. For example, your code could be a statement:
if (a == 4) { a = b; }
else { a = 5; }
Or you could write it with a conditional expression, but idiomatically like this:
a = (a == 4 ? b : 5);
Actually you should write
a = (a == 4) ? b : 5;
And that discussion about ternary ops has always seemed quite sterile to me - a couple of good insights can be found here (TL;DR OP's example:
return (a<b) ? (b<c) ? b : (a<c) ? c : a : (a<c) ? a : (b<c) ? c : b;
is probably a case where using the ternary operation is not a good idea, in cases like yours it contributes to readability if you ask me).
That is a bad way to use the ternary operator (there are worse ways, but that one is bad). You should use:
a = (a == 4) ? b : 5;
The difference is that this is clearly an assignment to a and the ternary operator determines the value to assign. In your version, you have to look at the insides of the ternary operator to see that it is assigning to a in both branches. (One of the worse ways of using the ternary operator would be (a == 4) ? a = b : b = 5; assigning to two different variables.)
If you feel the need to write nested ternary operators, then you're probably using it inappropriately, too. Occasionally it will be OK, but not very often.
The ?: operator can be used as a shortcut for an if...else statement. It is typically used as part of a larger expression where an if...else statement would be awkward.
For example:
time_t t = time(0);
now = localtime( & t );
greeting = "Good" + ((now->tm_hour > 17) ? " evening." : " day.");
copied from: http://msdn.microsoft.com/en-us/library/be21c7hw%28v=vs.94%29.aspx
This question already has answers here:
or is not valid C++ : why does this code compile?
(3 answers)
Closed 9 years ago.
What is the function of and() in C++ and its syntax?
P.S. I happened to write out and() as a function and the C++ text editor highlighted it.
Even after much searching I could not find its function or the syntax.
There is no and function in C++, it's a reserved identifier, the same as the logical operator &&.
C++11(ISO/IEC 14882:2011) §2.5 Alternative tokens
In C, there's no and keyword, but if you include the header iso646.h, and is the same as && as well.
C11(ISO/IEC 9899:201x) §7.9 Alternative spellings
The header <iso646.h> defines the following eleven macros (on the left) that expand
to the corresponding tokens (on the right):
and &&
and_eq &=
bitand &
bitor |
compl ~
not !
not_eq !=
or ||
or_eq |=
xor ^
xor_eq ^=
and is not a function; it is an operator. It means the same thing as &&. For example,
x && y
and
x and y
mean the same thing.
If you try to use it as a function, it will give you an error.
See this answer for more information on and, or, etc.