8,605 questions
Best practices
0
votes
3
replies
102
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, ...
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-...
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: ...
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
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 ...
2
votes
1
answer
201
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-&...
1
vote
0
answers
50
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> &...
1
vote
1
answer
128
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 ...
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
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
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>
...
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 ...
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 ...
3
votes
1
answer
269
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(...
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....
0
votes
0
answers
81
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 ...
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 {
// ...
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, ...
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)...
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
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 ...
0
votes
1
answer
59
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 ...
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 ...
6
votes
2
answers
185
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 ...
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-...