5

Is it possible to overload the operator new to be constexpr function? Something like:

constexpr void * operator new( std::size_t count );

The reason why would be to execute constexpr function within the overloaded operator body where count argument value would be an input data... As the operator is invoked by:

SomeClass * foo = new SomeClass(); 

The size of the data type is know at compile time, isn’t it? (count== sizeof(SomeClass)) So the count can be considered as compile time constant?

constexpr void * operator new( std::size_t count )
{
  if constexpr ( count >= 10 ) { /* do some compile-time business */ }
}

Many thanks in advance to anyone willing to help!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Martin Kopecký
  • 912
  • 5
  • 16
  • What's the intent here of using `constexpr`? Why not a template? – tadman Nov 21 '18 at 19:19
  • 4
    constexpr cannot have side effects, thus this would be contradictory – OznOg Nov 21 '18 at 19:19
  • BTW, you can have your constructor constexpr, thus I don't get the use case... – OznOg Nov 21 '18 at 19:21
  • My Task is to do custom O(1) memory pool based allocation. I need to evaluate the count at compile time to select appropriate memory partition having chunks of adequate size. – Martin Kopecký Nov 21 '18 at 19:24
  • The operator new can be made templated? – Martin Kopecký Nov 21 '18 at 19:24
  • 4
    There is no such thing as compile-time dynamic memory allocation. Allocating memory for your program at compile time is by definition just specifying a quantity of static memory necessary at runtime. If you can calculate at compile time you need `N` bytes at runtime for your task, then just have a properly aligned `static` buffer of size `N`. – François Andrieux Nov 21 '18 at 19:31
  • The memory chunks are made statically while the dynamic allocation itself is substituted by assigning the chunk from the memory pool... – Martin Kopecký Nov 21 '18 at 19:34
  • I can't see how this would work, conceptually. Either you know at compile time what address will be returned, or you don't. The first case is compatible with `constexpr`, the second with dynamic memory allocation. – Tim Randall Nov 21 '18 at 20:02
  • 5
    It doesn't matter anyway. [expr.const] prohibits *new-expression*s in constant expressions across the board. – T.C. Nov 21 '18 at 20:04
  • 6
    You want to look at the C++2a papers trying to make everything constexpr. – Marc Glisse Nov 21 '18 at 20:10
  • 1
    @FrançoisAndrieux Read this: http://open-std.org/JTC1/SC22/WG21/docs/papers/2018/p0784r1.html – Oliv Nov 21 '18 at 21:27
  • 1
    Man, these comments did *not* age well … – Konrad Rudolph Feb 26 '20 at 10:45

1 Answers1

4

You can't overload operator new to be constexpr, the main problem is attributed to the C++ standard directive §9.1.5/1 The constexpr specifier [dcl.constexpr] (Emphasis Mine):

The constexpr specifier shall be applied only to the definition of a variable or variable template or the declaration of a function or function template. A function or static data member declared with the constexpr specifier is implicitly an inline function or variable (9.1.6). If any declaration of a function or function template has a constexpr specifier, then all its declarations shall contain the constexpr specifier.

That is, in order to overload operator new all its previous declarations must also be constexpr, which they aren't and thus overloading it as constexpr you get a compile time error.

101010
  • 41,839
  • 11
  • 94
  • 168
  • 1
    I'm not 100% sure that this is correct. Doesn't this talk about the case when there are several declarations of the same function (with the same signature)? I don't really see the rationale behind, if one overload was `constexpr`, then all of them must be `constexpr` as well. – geza Nov 21 '18 at 20:19
  • Which version of standard has been mentioned? – Sergei Krivonos Sep 15 '19 at 14:11