Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[expr.const] p5 neither allocation nor deallocation function is a constexpr function #4947

Closed
xmh0511 opened this issue Sep 28, 2021 · 8 comments

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Sep 28, 2021

#include <iostream>
void* operator new(std::size_t n){
    return malloc(n);
}
constexpr int f() {
    auto ptr = new int();  // #1
    delete ptr;  // #2
    return 0;
}
int main() {
    constexpr int v = f();
}

The intent of [expr.const#5.17] and [expr.const#5.18] is to make #1 and #2 be usable in constant expressions. However, regardless of invocation of allocation or deallocation function, they all violate [expr.const#5.2]

An expression E is a core constant expression unless the evaluation of E, following the rules of the abstract machine ([intro.execution]), would evaluate one of the following:

  • an invocation of a non-constexpr function;

They are contradictory.

Proposal:

an invocation of a non-constexpr function, unless it is a replaceable global allocation/deallocation function invoked by a new-expression or a delete-expression.

@xmh0511 xmh0511 changed the title [expr.const] p5 neither allocation nor deallocation function are constexpr [expr.const] p5 neither allocation nor deallocation function is a constexpr function Sep 28, 2021
@cpplearner
Copy link
Contributor

[expr.new]:

During an evaluation of a constant expression, a call to an allocation function is always omitted.

@jensmaurer
Copy link
Member

Nothing to see here, I guess.

@xmh0511
Copy link
Contributor Author

xmh0511 commented Oct 25, 2021

@cpplearner @jensmaurer Ok, for "allocation function", it is omitted by [expr.new] p14, however, how about "deallocation function"? there is no special rule that omits it during the evaluation of a constant expression.

@xmh0511
Copy link
Contributor Author

xmh0511 commented Oct 25, 2021

@jensmaurer Should we also give a similar special rule in [expr.delete]?

During an evaluation of a constant expression, a call to a deallocation function is always omitted.

@jensmaurer
Copy link
Member

Not necessary. If the call to the allocation function is omitted, we also omit the call to the deallocation function for allocations so treated.

@xmh0511
Copy link
Contributor Author

xmh0511 commented Oct 25, 2021

I wonder which rule specifies that "If the call to the allocation function is omitted, we also omit the call to the deallocation function for allocations"?

@jensmaurer
Copy link
Member

[expr.delete] p7 bullets

@xmh0511
Copy link
Contributor Author

xmh0511 commented Oct 26, 2021

Thanks. I see

If the value of the operand of the delete-expression is not a null pointer value, then:

  • [...]
  • Otherwise, the delete-expression will not call a deallocation function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants