2,706 questions
-4 votes
1 answer
173 views
Why am I getting "nan" when I try to overload the modulo operator in C++? [closed]
I am trying to overload the % operator in C++ to make it do division instead of modulo. For example, 5 % 4 should give me 1.25 after overloading. But I get "nan". Why? This is the code I ...
1 vote
1 answer
161 views
Redefining new and delete operators
I want to redefine new and delete operators to use a custom allocator in a c++ project with multiple translation units. Here are the redefines written in the memops.hpp file: #pragma once #include &...
0 votes
1 answer
165 views
conditionally enable conversion operator with SFINAE
[Note: I'm stuck with C++17.] I have a class template that is instantiated over a fairly large number of types (~25). A few of them can correctly be converted among one another, and I would like to ...
3 votes
2 answers
197 views
Lifetime of temporary objects used while chaining overloaded member access operator->
According to what I know, the result value of an overloaded operator->() can be any type T provided that T is either a pointer type or it is a class/struct which also exposes an overloaded operator-...
3 votes
2 answers
142 views
Calling member arrow operator -> recursively
The member arrow operator is treated by the compiler in a special way. Since an overloaded arrow operator can have an arbitrary return type, several such operators from different classes may need to ...
2 votes
3 answers
171 views
C++ How to define the spaceship operator for a template class?
Im trying to define the spaceship operator for a simple template string class. namespace mylib { template <size_t N> struct String { String() { *data = '\...
7 votes
1 answer
175 views
Wrong computation of sum in a constant expression
My program after porting to Visual Studio shows some weird results. After reduction I came to this minimal reproducible example: consteval auto operator +( auto x, auto&& y ) { return x += ...
0 votes
1 answer
98 views
Is it possible to invoke a different version of an overloaded class operator based on whether the call is made from inside or outside the class?
I am developing quite a large Class whose objects require relatively costly validation if they are created by the public constructors, but for which the validation can be dispensed with for private ...
2 votes
0 answers
88 views
Silo compilation error: no match for ‘operator<<’
Compile Silo and get the following error error: no match for ‘operator<<’ (operand types are ‘std::ostringstream’ {aka ‘std::__cxx11::basic_ostringstream<char>’} and ‘std::pair<const ...
0 votes
0 answers
85 views
C++ operator== doesn't work but an actual function does [duplicate]
I got a function: bool equal(int var, std::initializer_list<int> list) { return std::find(list.begin(), list.end(), var) != list.end(); } which works, I got no error when I use the function....
-1 votes
1 answer
143 views
Precedence of pre-increment operation over post-increment operation [duplicate]
package test; public class Test { public static void main(String [] args) { int a = 10; int b = 12; System.out.println( ++a == 11 || b++ == 12); ...
3 votes
2 answers
123 views
Class conversion operator is not called implicitly
In the program below, class A has a convert-to-int operator. Class B has a convert-to-A operator. While the compiler converts A to int implicitly, it fails to do so for converting B to A: class A { ...
-1 votes
1 answer
235 views
Cython : How can we properly call add, sub, mul & divide operator?
In .pxd file, I wrote _Point operator+(const _Point other) const _Point operator -(const _Point other) const bool operator==(const _Point other) const In .pyx file, I wrote def __eq__(...
4 votes
1 answer
227 views
Can I mark a function with noexcept if it has an argument passed by copy?
So I wanted to improve the noexcept of some operators and functions in my code can I mark this function noexcept ? std::string remove_suffix(/*passed by copy*/ std::string s) /*noexcept*/ // is ...
1 vote
1 answer
59 views
Python operate 'in' on tuple won't match by whole word [duplicate]
It seems the operator 'in' on tuple won't only match the whole string if there is only one item in the tuple. The behavior is different from 'list' and 'set'. I don't understand where the difference ...