Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Filter by
Sorted by
Tagged with
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, ...
Ben Voigt's user avatar
  • 286k
-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 ...
JAYD3V's user avatar
  • 12.6k
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-...
La Creatura's user avatar
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 ...
Mitten.O's user avatar
  • 1,107
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: ...
barnabas's user avatar
  • 157
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; // ...
PiotrNycz's user avatar
  • 24.9k
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) { ...
xmllmx's user avatar
  • 44.5k
-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 ...
JHBonarius's user avatar
  • 11.5k
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 ...
JHBonarius's user avatar
  • 11.5k
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 ...
Will Woolfenden's user avatar
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-&...
darune's user avatar
  • 11.5k
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 ...
Chris Ri's user avatar
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> &...
Makogan's user avatar
  • 9,981
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&,...
Tyler Pruden Jin2188's user avatar
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 ...
sakcakoca's user avatar
  • 129
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<<...
ramgorur's user avatar
  • 2,191
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 } [...
iRon's user avatar
  • 24.4k
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 ...
Maddie's user avatar
  • 19
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 = ...
Stas Badzi's user avatar
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 ...
Albertus's user avatar
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> ...
Topological Sort's user avatar
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 ...
Stef1611's user avatar
  • 2,515
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 ...
user200783's user avatar
  • 14.5k
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 ...
glades's user avatar
  • 5,346
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 ...
Giogre's user avatar
  • 1,664
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 = ...
Andrus's user avatar
  • 28.2k
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(...
Jaka's user avatar
  • 1,241
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 ...
minomy13's user avatar
  • 130
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....
Boris's user avatar
  • 39
-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 ...
KPathak's user avatar
  • 49
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 ...
KPathak's user avatar
  • 49
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'...
Avinash's user avatar
  • 11
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 { // ...
dEmigOd's user avatar
  • 640
-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 ...
bourne's user avatar
  • 1,259
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, ...
Paul Grinberg's user avatar
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 ...
Bartek_0x00's user avatar
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)...
Stef1611's user avatar
  • 2,515
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 ...
Aashishkebab's user avatar
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)...
SHACHA's user avatar
  • 1
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 ...
Markus W.'s user avatar
  • 392
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 ...
Asmoox's user avatar
  • 662
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> #...
Daniel's user avatar
  • 13
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 ...
dscerutti's user avatar
  • 333
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 ...
Eternal Dreamer's user avatar
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 ...
glades's user avatar
  • 5,346
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 ...
Eliott Martin's user avatar
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 ...
Paul Sheppard's user avatar
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()() # ...
Lucas's user avatar
  • 65
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-...
FangQ's user avatar
  • 1,564
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: ...
kesarling's user avatar
  • 2,322

1
2 3 4 5
173