2

In C, there is the concept of a compound literal which can be used to take any literal and make it addressable. For instance:

int* p = &(int){0};

Compound literals do not exist in C++. Is there another construct in C++ that can turn a literal into an lvalue? For example:

// Something semantically equivalent to this, but without the need of a declaration
int n = 0;
int* p = &n;

EDIT:

Having a function or type to create the solution is fine, as long as no more than a single inline statement is needed to facilitate its use. For example, int* p = new int(0); requires two lines per instance of use: one line where it is used, and one line at the end of scope to delete the allocation.

Solution must be doable in standard C++; no extensions.

Stewart Smith
  • 1,396
  • 13
  • 28
  • 4
    Is there any good reason why you want to do this? – WhiZTiM Apr 29 '18 at 18:53
  • There are instances when using OpenGL where I want to pass many literals by pointer. I could make declarations, but an inline value I can use and forget about would be less bloat. – Stewart Smith Apr 29 '18 at 18:56
  • Well an extra layer of indirection may not hurt. You can write wrappers for that; But see [this](https://stackoverflow.com/questions/28116467/are-compound-literals-standard-c) if you want to use non-standard extensions – WhiZTiM Apr 29 '18 at 19:00
  • `int* a = new int(3);` Something like this? – artur99 Apr 29 '18 at 19:02
  • @artur99 This adds to my problem, since I would still need two lines (statement + a delete at the end of scope), but now have to make sure I'm not leaking memory. – Stewart Smith Apr 29 '18 at 19:05
  • @WhiZTiM Good to know, but unfortunately my environment prohibits extensions. The solution needs to be standard C++. – Stewart Smith Apr 29 '18 at 19:06
  • *"using OpenGL where I want to pass many literals by pointer"* could you provide some example code? This sounds like you фку scattering magic numbers around. – user7860670 Apr 29 '18 at 19:21
  • @VTT I will have to get back to you with the code snippet. Regardless of where it is deemed a good use case or not, the question asks if there is some equivalent to compound literals in C++. – Stewart Smith Apr 29 '18 at 19:36
  • Do you need the lifetime of the object to extend for more than one statement, or is it just passing a pointer to a (C-style) function that will use it only during the call (like typical `const&` parameters)? – Davis Herring Apr 29 '18 at 20:06
  • @Davis Herring Your c style assumption is correct. It only needs to exist for the call like a const& parameter. – Stewart Smith Apr 29 '18 at 20:11
  • Instead of `int*` you can use `unique_ptr` and it will magically delete itself at the end of the scope. – Bo Persson Apr 29 '18 at 20:24
  • @BoPersson This meets my criteria, but would cause an unnecessary heap allocation. – Stewart Smith Apr 29 '18 at 20:52

1 Answers1

2

You can define

template<class T>
T& lvalue_cast(T &&t) {return t;}

which accepts literals (among other things). The temporary lasts until the end of the complete expression lexically containing its creation (presumably materialization to bind the reference for lvalue_cast). Obviously restricting its use to that interval is up to you.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • 3
    It's probably worth expanding on the lifetime effects. In C, a compound literal inside a function dies when the enclosing block ends; I believe this would die much sooner. – user2357112 Apr 29 '18 at 20:32
  • @user2357112: Sure—I checked on the needed lifetime in a comment, but I mentioned it here now too. – Davis Herring Apr 29 '18 at 20:45