27 questions
5
votes
1
answer
148
views
Can explicit object member functions be called on a pointer to allocated storage?
Consider the following: https://godbolt.org/z/o5r7jf4rK
#include <memory>
struct Ignore {
constexpr Ignore(auto&& unknown_reference) {}
};
struct X {
constexpr bool f1() { ...
6
votes
2
answers
204
views
Why does Clang 20.1.2 with C++26 fail to find a derived class method when using explicit object parameters as a CRTP replacement?
I'm trying to use C++23's explicit object parameters (as introduced in P0847R7, "Deducing this") to replace the Curiously Recurring Template Pattern (CRTP) for a simple inheritance hierarchy,...
17
votes
0
answers
371
views
"Deducing this" causes `exception specification is not available until end of class definition`, is this a compiler bug?
Consider the following code:
struct A
{
void foo(this auto &&) noexcept(true) {}
auto bar() -> decltype(foo()) {}
};
Clang 19 rejects this code with the error below, while GCC and ...
4
votes
1
answer
191
views
Comparison operator with explicit object parameter of not class type
Can comparison operators for a class in C++23 have explicit object parameter of a type distinct from the class type?
Consider for example
struct A {
int i;
constexpr bool operator==(this int x,...
6
votes
1
answer
162
views
Overload resolution for copy assignment operator
In addition to an implicitly-defined copy assignment operator, if a class also defines an operator= without an lvalue-reference object parameter, which of the operators must be selected?
Please ...
2
votes
1
answer
127
views
Defaulting comparison operator with explicit object parameter
In C++23 a class can have explicit object member functions (with the first parameter prefixed by this), including member comparison operators. Which of them a compiler can generate automatically after ...
7
votes
1
answer
152
views
Inaccurate compile-time computation of Fibonacci sequence in a recursive lambda
Below is a recursive lambda expression that can compute the values of Fibonacci sequence both in runtime and during constant evaluation:
auto fib = [](this auto && f, auto && p) {
...
4
votes
1
answer
127
views
Why does operator() copy movable temporaries in Clang?
In the following C++23 program
struct A {
A() {}
A(A&&) = default;
void f(this A) {}
void operator() (this A) {}
};
int main() {
A{}.f(); // ok
A{}(); // Clang error
...
4
votes
2
answers
171
views
Move elision in explicit object member functions
If one calls explicit object member function of a temporary, must the move of the temporary be elided in the explicit object parameter?
Consider the following example, where struct A has move ...
2
votes
1
answer
122
views
Overload resolution between ordinary and explicit object member functions
In the following test program, struct B has two member functions f, which can be called using B{}.f(): one ordinary f() and another with explicit object f(this A).
struct A {
int f() { return 1; }
...
6
votes
2
answers
233
views
Explicit object member function with void parameter
According to cppreference since C++23
For a non-static non-virtual member function not declared with cv-qualifier or ref-qualifier, its first parameter, if not being a function parameter pack, can be ...
11
votes
2
answers
740
views
Explicit this object parameter wonkiness
There was some code floating around on Reddit that defined a member-function with an explicit this object parameter defined as type int. This made me wonder how this member-function could possibly be ...
7
votes
1
answer
372
views
MSVC rejects program with member function call while gcc and clang accept
I wrote the following program in C++23 that compiles with gcc and clang but is rejected by msvc. I want to know is this well-formed or ill-formed etc as per the standard. Live demo
struct C
{
void ...
2
votes
1
answer
118
views
A uniform way to call a member function from both explicit and implicit object member functions
TL;DR
Is there a way to have the same code snippet (macro), that calls a given member function from another member functions, which might have both explicit or implicit object parameters? Something ...
19
votes
1
answer
2k
views
Combined with C++23 Deducing this and conversion operator with auto return type?
I recently noticed a strange issue regarding C++23 Deducing this feature.
Suppose we have a struct S with a simple conversion operator:
struct S {
operator int() { return 42; }
};
int i = S{};
...
2
votes
3
answers
439
views
Why is it a syntax error to use explicit object parameters in a function pointer?
So I've run into a problem when trying to use explicit object parameters for a function pointer, for a project I'm working on.
The code at a glance seems syntactically fine but as this is a new ...
5
votes
1
answer
337
views
Lambda with explicit object parameter and std::invoke_result_t
How do I get the return type of a lambda that has a deducing this signature using std::invoke_result_t.
auto lambda = [](this auto& self, int x) -> int {
return x;
};
auto x = std::...
2
votes
2
answers
107
views
How to keep from duplicating methods in volatile classes
Suppose I have the following very simple class:
class A
{
public:
static constexpr A make() { return A{}; }
constexpr A() : _v(0) {}
constexpr A& setV(int v) { _v = v; return *this; }...
1
vote
1
answer
138
views
Will using Template Typdefs force us to duplicate iterator objects in C++23 even though "Deducing this" eliminates this problem?
One of the problems Deducing this solves is duplication by making member functions cvref aware of the cvref-ness of the object the function is being called on. By declaring Alias templates (Template ...
0
votes
1
answer
196
views
Implementing a "virtual" method returning *this (covariant return type)
I'm writing a hierarchy of classes of C++, let's say A, B inheriting A, C inheriting A, and D inheriting B.
Now, all of these classes must have a method bar() &, whose body is:
{
A::foo();
...
14
votes
2
answers
1k
views
How to perfectly forward `*this` object inside member function
Is it possible to perfectly forward *this object inside member functions? If yes, then how can we do it? If no, then why not, and what alternatives do we have to achieve the same effect.
Please see ...
4
votes
1
answer
248
views
Does Explicit Object Parameter Allow Convertible Types?
From §4.2.7 of the proposal http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0847r7.html#pathological-cases
It said that:
These are even more unlikely to be actually useful code. In this ...
3
votes
1
answer
231
views
Accessing captured variables through explicit this parameter in lambda
From declarations / functions / 9.3.4.6 / 6.2 (i apologize on how to cite the specific sentence from standard):
An explicit-object-parameter-declaration is a parameter-declaration with a this ...
12
votes
1
answer
5k
views
What does explicit *this object parameter offer in C++23?
In C++23, deducing this is finally added to the standard.
Based on what I've read from the proposal, it opens up a new way of creating mixins, and possible to create recursive lambdas.
But I'm ...
2
votes
2
answers
1k
views
P0847 deducing this - can it allow a generic clone without a need for CRTP?
P0847 proposes the possibility of using explicit this parameter for member functions.
Among other great goodies that this proposal brings, there is also the great new possibility for CRTP without C, R ...
4
votes
2
answers
182
views
Avoiding repetition of const and non-const version of getters?
struct BananaHolder
{
vector<Banana>& getBananas();
const vector<Banana>& getBananas() const;
};
My classes are cluttered with this kind of duplication.
Is there a cleaner,...
329
votes
22
answers
61k
views
How do I remove code duplication between similar const and non-const member functions?
Let's say I have the following class X where I want to return access to an internal member:
class Z
{
// details
};
class X
{
std::vector<Z> vecZ;
public:
Z& Z(size_t index)
...