8,605 questions
Best practices
0
votes
3
replies
99
views
Testing equality and inequality comparison
Philosophically, does GoogleTest's ASSERT_EQ(x, y) mean "check that x is equal to y" or "evaluate x == y and check that the result is truthy"?
In the former case, then
struct S s1, ...
-3
votes
1
answer
112
views
How to overload the newish 3-way operator for a simple class whose value is contingent on 2 numeric properties
Overloading 3-way comparison operator.
I am a bit confused when it comes to the 3-way comparison operator. I don't understand why the two return types, that I listed below, are used with the 3-way ...
1
vote
1
answer
133
views
Unexpected overload resolution on clang [duplicate]
(I was making an integer class to replace C's stuff, then this hit me. I wanted an overload for pointer arithmetic.)
MSVC and GCC pass without errors or warnings.
Compilation:
g++ -std=c++2c -fsyntax-...
19
votes
2
answers
821
views
How can a longer C++ implicit conversion chain succeed and a strict subchain of it fail in operator lookup?
Background
Our original problem was that including boost::logic::tribool caused an expression of the form referenceToClass != nullptr to compile happily. We ultimately tracked this down to the ...
5
votes
2
answers
161
views
Is this assignment to brace-enclosed string constant really illegal in C++?
I am porting a large codebase from Linux/g++ to MacOS/Clang. I hit this compiler error in multiple places in Clang (where g++ builds successfully and does the right thing at run time):
error: ...
3
votes
1
answer
170
views
Any usages of default operator!=, operator< ...?
Let simplify to operator== and operator!=.
If I have ..operator==..=default; then I do not need to have the operator!= to be defaulted.
struct S {
bool operator==(const S&) const = default;
// ...
0
votes
0
answers
107
views
Why does std::equality_comparable_with not work in the simplest case? [duplicate]
#include <concepts>
struct A {};
struct B {};
constexpr bool operator==(A, A) {
return true;
}
constexpr bool operator==(B, B) {
return true;
}
constexpr bool operator==(A, B) {
...
-1
votes
1
answer
74
views
Prevent duplicate boilerplate operator overloads [duplicate]
This is a follow up of this question, which might have been a XY problem. Anyhow, that was closed within a few minutes, before I could get any real help.
In our large code base (measurement processing ...
1
vote
2
answers
248
views
Prevent duplicate operator overloads implementations
In our large code base (measurement processing application) we've had bugs where values were mixed up because everything is a double. So we decided to try-out strongly named types for things like ...
1
vote
1
answer
43
views
Can I replace an operator in a Julia function when wrapped for testing?
I'm writing a Julia package for testing the behaviour of floating point addition.
module TestModule
const base_add = Base.:+
# used for storing information when addition is done
mutable struct ...
2
votes
1
answer
200
views
What's the reason for std::reference_wrapper to not overload operator->?
A std::reference_wrapper is guaranteed to be initialized (but can be rebound, unlike a normal reference).
Being a wrapper class, what's the reason for std::reference_wrapper not to overload operator-&...
2
votes
3
answers
186
views
Multiple namespaces overloaded operator<<, how can I use them?
I'm reading a book. Obviously there are some failures in it.
In the code below, how can I access plant::operator<< in run() and plant::debug::operator<< in diagnostic()? The compiler can't ...
1
vote
0
answers
49
views
Why does the compiler fail to find the correct function? [duplicate]
I have this generic function:
pub fn bounding_circle<V>(
points: &dyn Fn(usize) -> V,
point_count: usize,
)
where
V: InnerSpace<f32> + Debug + Copy,
for<'x> &...
0
votes
1
answer
112
views
error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'const cs10b_fraction::Fraction')
I get this error even though I overloaded the << operator.
My prototype is as follows, placed within the Fraction class header file.
friend std::ostream& operator << (std::ostream&,...
1
vote
1
answer
127
views
operator<< overload for std::errc is not detected in a std::formatter specialization for custom class in C++
I have a template class and a custom std::formatter specialization for that class to be able to format and print it. When I create this class with std::errc data and try to print it, the compiler ...
0
votes
0
answers
45
views
Explanation of a variadic parameter while overloading << operator for std::tuple [duplicate]
I was going through an example of how to overload operator<< for std::tuple in this cpp reference link.
Here is the example code:
template<typename... Ts>
std::ostream& operator<<...
3
votes
1
answer
120
views
How to implement a case-sensitive `Equals` for operators as `-ceq`
An Equals method on a specific class might be used to implement a PowerShell operator as -eq:
class TestClass {
hidden [String] $_Value
TestClass([String]$Value) { $this._Value = $Value }
[...
1
vote
2
answers
134
views
C++: Overloading = operator for a linked list queue? [closed]
I'm trying to figure out why this operator is giving me trouble. I actually allowed Visual Studio to build the function definition for me, so I'm not sure why it's upset at me. There's also an issue ...
1
vote
0
answers
74
views
Why is `operator=` automatically casting to `const type` [duplicate]
When I use this code:
#include "promise.hpp"
#include <thread>
using namespace std;
int main() {
stsb::promise<int> tstint = stsb::promise<int>();
thread waitthr = ...
0
votes
2
answers
96
views
Visual Studio error C2953 when redefining operator << with template parameter packs
All,
I ran into a C++20 Visual Studio error C2593 ("Operator << is ambiguous") when compiling this simple program patterned from section 4.2 of the Vandevoorde, Josuttis and Gregor ...
0
votes
2
answers
103
views
C++ nonmember binary operator with private inheritance
As has been pointed out elsewhere, the community thinks C++ binary operators like == should be nonmember functions. This presents a problem for me with private inheritance.
#include <cassert>
...
1
vote
1
answer
134
views
How to use openmp reduction on a derived type array in fortran?
Since OpenMP 4.5, arrays can be reduced using pragmas. But, I faced a problem when I tried to use it on arrays of derived type because !$OMP DECLARE REDUCTION is not found for this type. I understand ...
0
votes
1
answer
59
views
In Dart, can I implement `operator *` as in `2 * MyClass()`?
In Dart, I can implement operator * that is called when someone writes MyClass() * 2:
class MyClass {
String operator * (int rhs){
return 'MyClass * int';
}
}
Is it possible to write a ...
4
votes
1
answer
76
views
Store value category of pointed-to object (for later deref)
Ok this might be a bit difficult to explain:
I want to call a function hello that has an overload for either an rvalue or an lvalue ref of type Json. This Json object is converted from a JsonKey using ...
3
votes
1
answer
172
views
Overloading comparison operators for simple POD `struct`s holding many numerical fields
Say I have got a plain old data (POD) struct containing multiple numeric fields.
I want to overload the comparison operators so that I can compare two instances of my POD.
The problem is that I need ...
0
votes
1
answer
129
views
How to create operator if not exists
Query
CREATE OR REPLACE FUNCTION public.concatkeepspaces(left bpchar, right bpchar)
RETURNS bpchar
LANGUAGE sql IMMUTABLE
AS $BODY$
SELECT concat($1,$2);
$BODY$;
CREATE OPERATOR public.+ (
leftarg = ...
3
votes
1
answer
265
views
operator== ambiguity in C++20
Why is this code ambiguous starting with C++20?
template <typename T>
struct base_widget {
bool operator==(T const&) const;
};
struct gadget : base_widget<gadget> {};
bool foo(...
0
votes
0
answers
18
views
Using generic constants and AsRef in operator overloading [duplicate]
So, at the moment, I'm implementing a matrix. This
fn multiply<const P: usize, T>(&self, rhs: T) -> Matrix<R, P>
where
T: AsRef<Matrix<C, P>>,
is the signature of ...
1
vote
0
answers
65
views
Why aren't there operator overloads for working with BigInteger and BigDecimal as part of the BigInteger and BigDecimal class imports?
...such as
infix operator fun BigInteger.plus(other: Int) =
this + other.toBigInteger()
infix operator fun Int.plus(other: BigInteger) =
toBigInteger() + other
infix operator fun BigInteger....
-1
votes
1
answer
64
views
Compilation issue when conversion operator is added
The following piece of code compiles fine
#include <iostream>
#include <map>
#include <memory>
using namespace std;
class child {
public:
child(std::string name,uint32_t ...
0
votes
0
answers
80
views
Compilation issue with subscript operator overloading when another conversion operator is added [duplicate]
The following piece of code compiles fine
#include <iostream>
#include <map>
#include <memory>
using namespace std;
class child {
public:
child(std::string name,uint32_t ...
1
vote
2
answers
184
views
In C++, is the overloading of the arrow operator an exceptional case or does it follow the same logic as the case for other operators?
I have a question regarding the arrow operator's overload process. The overloading makes it seem like it is a unary operator, while the usage makes it appear as a binary operator. The return value isn'...
2
votes
2
answers
70
views
Operator overloading resolution in the hierarchy of classes
I have an hierarchy with an operator() overloading like the following
class Base{
public:
virtual std::vector<float> operator()(const std::vector<float>& us) const {
// ...
-1
votes
1
answer
73
views
Clarification regarding operator overloading
To provide context I have an executable that depends on another Foo library that provides an implementation for the Identifier class, now module class in my executable needs to cache the Foo object ...
0
votes
1
answer
49
views
operator<< overload for class with multiple layers [duplicate]
I am trying to get to the point where essentially MyClass(A) << "hi " << 17 << std::endl; compiles and executes MyClass::finish() method on the accumulated stream. So far, ...
0
votes
1
answer
104
views
Faking template virtual function in C++
I would like to simplify this convoluted piece of code:
class Device {
protected:
int base;
int size;
// other
public:
virtual uint64_t load(uint64_t addr, uint64_t size) = 0;
virtual void ...
2
votes
0
answers
116
views
Is it possible in new fortran version (2018, 2023) to overload assignment operator with an array at the LHS?
I read the following post : access operators "[ ], ( ), { }" overloading in Fortran 90 or 2003 and I would like to know if the situation has changed in new fortran versions (2008, 2018, 2023)...
2
votes
1
answer
116
views
Are .equals() and == the same thing in Kotlin?
If I try to override both the equals function and the == operator through override operator fun equals, I get an error of "Conflicting overloads".
Does this mean that == and .equals() are ...
0
votes
2
answers
75
views
Override -- operator in C#
I try to overide -- operator.
I want prefix and postfix -- operator
but c# doesnot allowed it even that there is different signature's function.
the code:
public static Rational operator --(Rational a)...
0
votes
1
answer
86
views
Expanding using declaratives for a conversion operator
The following code does not compile. What would be the correct syntax, and is it available?
I let struct overloaded inherit from its transformed template arguments, wrapping every argument into ...
0
votes
0
answers
71
views
Hooking std::plus<T>() to overloaded operator+
I practice easy coding on Codewars platform. There is a place where I found following exercise:
Write a function that accepts two square matrices (N x N two
dimensional arrays), and return the sum of ...
1
vote
1
answer
127
views
operator<< overload not selected for rvalue std::ostringstream and std::unique_ptr
I noticed that the std::basic_ostream operator<< overload for rvalue ostreams is not selected for a temporary std::ostringstream in combination with std::unique_ptr.
#include <iostream>
#...
0
votes
1
answer
58
views
Is there an efficient way to overload operators for structs when the underlying data is piecewise?
I have various methods for accumulating quantities in fixed-precision numerics based on the following data type:
int95_t {
long long int x;
int y;
};
The numbers count forward in x until it may ...
1
vote
3
answers
153
views
Ignore the use of a C++ function during compilation having operator<<
I'm developing an API looking like GLib logging.
My API can also disable these functions when the flag -DDEBUG isn't passed to GCC.
That means, there are no residues in the binary, all these debug ...
3
votes
1
answer
120
views
Why isn't the compiler able to deduce type of std::endl for my operator<<(T&&)?
I'm using perfect forwarding to pipe a type through to a associated JsonRecord object. However, compiler is unable to deduce the type of std::endl when I want to pick it up as a token to forward to ...
0
votes
1
answer
114
views
Issue with combining __declspec(property) with operator[]
I don't understand why in this C++ program the [0] and [19] in (b.a_test)[0][12][19] get ignored, while on the next line they aren't.
Could anyone exlpain to me why?
#include <iostream>
class A ...
6
votes
2
answers
184
views
Using base class operator= overload not building
In this example code, why isn't
using IParameterBase<TYPE>::operator=;
working, and the base class assignment operator working?
I recently changed to this templated version, previously I had ...
0
votes
2
answers
125
views
In js, is there any feature can implement callable instance, like __call__ method in python [duplicate]
In python, you can make an instance become a callable obj, like below shows:
# py code snippet
class Demo:
def __call__(self):
print("Demo's instance callable")
Demo()() # ...
4
votes
2
answers
155
views
multi-level/recursive assignment subsasgn example in matlab
I am writing a matlab class and would like to overwrite the subasgn operator to achieve this
obj.('elem1').('subelem1').('subsubelem1')=val;
where the level of depth can vary. This type of multi-...
0
votes
2
answers
92
views
How do I overload a certain operator in haskell to take different types on either side?
MRE:
class Foo s where
myCons :: Char -> s -> s
myCons c xs = <my definition of however I wish to interpret this>
instance (Eq, Show) Foo where
(:) x y = x `myCons` y
Error:
...