Date: Fri, 4 Jul 2025 11:40:19 +0200
For the delete case, C++26 has the syntax:
void foo() = delete("This function has been replaced.");
For handing the consteval case, perhaps a nicer solution would be to
allow overloading a function in the consteval and non-consteval cases.
That would let you write :
consteval const char* c_str() const { return ... }
const char* c_str() const = delete("You can't do that.");
In most uses, distinguishing between the compile-time context and
runtime context can be done with "if consteval", but the non-consteval
branch is not allowed to have compile-time errors such as calling a
deleted function, or using static_assert(false).
(Maybe there is already a way to handle that which I haven't thought of.)
On 04/07/2025 10:36, Frederick Virchanza Gotham via Std-Proposals wrote:
> On Fri, Jul 4, 2025 at 3:17 AM JJ Marr wrote:
>>
>> consteval const char* c_str() const { return define_static_string(*this); }
>
>
> I think this is a really cool idea except for when a naive person
> encounters a compiler error for trying to use it at runtime. Ideally
> the line of code would be something like:
>
> consteval char const *c_str() const
> [[explain : consteval : "This method is consteval and cannot be
> used at runtime as "
> "it would require dynamic memory
> allocation to append a null "
> "terminator to the string
> (basic_string_view doesn't have a "
> "null terminator)" ]]
> {
> return define_static_string(*this);
> }
>
> And so then the compiler error would tell the programmer exactly why
> they can't use it. I'd also like to see the same kind of thing for
> when a method is 'delete'ed, e.g.:
>
> MyClass::MyClass(MyClass &&) = delete
> [[explain : delete : "This constructor is deleted because you
> cannot move a "
> "synchronisation object without affecting
> other threads "
> "that may be waiting on it " ]];
void foo() = delete("This function has been replaced.");
For handing the consteval case, perhaps a nicer solution would be to
allow overloading a function in the consteval and non-consteval cases.
That would let you write :
consteval const char* c_str() const { return ... }
const char* c_str() const = delete("You can't do that.");
In most uses, distinguishing between the compile-time context and
runtime context can be done with "if consteval", but the non-consteval
branch is not allowed to have compile-time errors such as calling a
deleted function, or using static_assert(false).
(Maybe there is already a way to handle that which I haven't thought of.)
On 04/07/2025 10:36, Frederick Virchanza Gotham via Std-Proposals wrote:
> On Fri, Jul 4, 2025 at 3:17 AM JJ Marr wrote:
>>
>> consteval const char* c_str() const { return define_static_string(*this); }
>
>
> I think this is a really cool idea except for when a naive person
> encounters a compiler error for trying to use it at runtime. Ideally
> the line of code would be something like:
>
> consteval char const *c_str() const
> [[explain : consteval : "This method is consteval and cannot be
> used at runtime as "
> "it would require dynamic memory
> allocation to append a null "
> "terminator to the string
> (basic_string_view doesn't have a "
> "null terminator)" ]]
> {
> return define_static_string(*this);
> }
>
> And so then the compiler error would tell the programmer exactly why
> they can't use it. I'd also like to see the same kind of thing for
> when a method is 'delete'ed, e.g.:
>
> MyClass::MyClass(MyClass &&) = delete
> [[explain : delete : "This constructor is deleted because you
> cannot move a "
> "synchronisation object without affecting
> other threads "
> "that may be waiting on it " ]];
Received on 2025-07-04 09:40:25