7

I would basically write the following piece of code. I understand why it can't compile.

A instance; // A is a non-default-constructable type and therefore can't be allocated like this

if (something)
{
    instance = A("foo"); // use a constructor X
}
else
{
    instance = A(42); // use *another* constructor Y
}

instance.do_something();

Is there a way to achieve this behaviour without involving heap-allocation?

4
  • Did you also provide a copy constructor and an assignment operator for A? Also for this case you should better have getter/setter functions for your class attributes, and have a default constructor. Mar 27, 2015 at 12:42
  • The easiest solution seems to be using a pointer. Is there a reason you need it to be on the stack?
    – CompuChip
    Mar 27, 2015 at 12:44
  • Push it into a vector? Mar 27, 2015 at 12:46
  • 1
    You can't do A instance("");?
    – Galik
    Mar 27, 2015 at 12:56

5 Answers 5

12

There are better, cleaner ways to solve the problem than explicitly reserving space on the stack, such as using a conditional expression.

However if the type is not move constructible, or you have more complicated conditions that mean you really do need to reserve space on the stack to construct something later in two different places, you can use the solution below.

The standard library provides the aligned_storage trait, such that aligned_storage<T>::type is a POD type of the right size and alignment for storing a T, so you can use that to reserve the space, then use placement-new to construct an object into that buffer:

std::aligned_storage<A>::type buf;
A* ptr;
if (cond)
{
  // ...
  ptr = ::new (&buf) A("foo");
}
else
{
  // ...
  ptr = ::new (&buf) A(42);
}
A& instance = *ptr;

Just remember to destroy it manually too, which you could do with a unique_ptr and custom deleter:

struct destroy_A {
  void operator()(A* a) const { a->~A(); }
};
std::unique_ptr<A, destroy_A> cleanup(ptr);

Or using a lambda, although this wastes an extra pointer on the stack ;-)

std::unique_ptr<A, void(*)(A*)> cleanup(ptr, [](A* a){ a->~A();});

Or even just a dedicated local type instead of using unique_ptr

struct Cleanup {
  A* a;
  ~Cleanup() { a->~A(); }
} cleanup = { ptr };
6
  • 1
    Okay, this is much better than my weak placement-new attempt. Mar 27, 2015 at 12:52
  • Who is going to call a destructor after placement constructor?
    – GreenScape
    Mar 27, 2015 at 12:53
  • 1
    @LightnessRacesinOrbit, yeah, it avoids potential alignment problems if the char buffer isn't suitably aligned Mar 27, 2015 at 13:01
  • I did. It's a manual conditional expression/lambda, which is just dumb. juanchopanza seems the be the only one noticing the obvious, language-provided solution.
    – Puppy
    Mar 27, 2015 at 13:08
  • 3
    @Puppy, the question asks how to reserve space on the stack, so that's what I answered. If the type is move-constructible and a conditional expression would work, that's what the OP should use, but if the two branches of the condition require multiple statements then the aligned_storage+placement-new solution still works. Mar 27, 2015 at 13:10
9

Assuming you want to do this more than once, you can use a helper function:

A do_stuff(bool flg)
{
  return flg ? A("foo") : A(42);
}

Then

A instance = do_stuff(something);

Otherwise you can initialize using a conditional operator expression*:

A instance = something ? A("foo") : A(42);

* This is an example of how the conditional operator is not "just like an if-else".

4
  • 2
    Then it has to be copy-constructable. Mar 27, 2015 at 12:47
  • @user3528438 Or move-constructible. But OP doesn't have requirements re. either of those. Mar 27, 2015 at 12:48
  • 1
    And if it's truly a one-off function, it can be a lambda. Mar 27, 2015 at 12:49
  • @Angew Or just A instance = something ? A("foo") : A(42); Mar 27, 2015 at 12:51
3

In some simple cases you may be able to get away with this standard C++ syntax:

A instance=something ? A("foo"):A(42);

You did not specify which compiler you're using, but in more complicated situations, this is doable using the gcc compiler-specific extension:

A instance=({
       something ? A("foo"):A(42);
});
4
  • More on " complicated situations", please? In what situation doesn't the first form work? Mar 27, 2015 at 12:49
  • @user3528438, if you need entire statements before creating the A objects. The conditional operator only allows expressions, the GCC extension allows arbitrary blocks. Mar 27, 2015 at 12:54
  • @user3528438: You pointed out one problem yourself on juanchopanza's answer: if A is not copy-constructible then this code is illegal. Mar 27, 2015 at 12:54
  • That's what lambdas are for or even a helper function. And it's move-constructible, scrub.
    – Puppy
    Mar 27, 2015 at 13:07
3

This is a job for placement new, though there are almost certainly simpler solutions you could employ if you revisit your requirements.

#include <iostream>

struct A
{
    A(const std::string& str) : str(str), num(-1)  {};
    A(const int num)          : str(""),  num(num) {};

    void do_something()
    {
        std::cout << str << ' ' << num << '\n';
    }

    const std::string str;
    const int num;
};

const bool something = true;   // change to false to see alternative behaviour

int main()
{
    char storage[sizeof(A)];
    A* instance = 0;

    if (something)
        instance = new (storage) A("foo");
    else
        instance = new (storage) A(42);

    instance->do_something();
    instance->~A();
}

(live demo)

This way you can construct the A whenever you like, but the storage is still on the stack.

However, you have to destroy the object yourself (as above), which is nasty.


Disclaimer: My weak placement-new example is naive and not particularly portable. GCC's own Jonathan Wakely posted a much better example of the same idea.

3
std::experimental::optional<Foo> foo;
if (condition){
  foo.emplace(arg1,arg2);
}else{
  foo.emplace(zzz);
}

then use *foo for access. boost::optional if you do not have the C++1z TS implementation, or write your own optional.

Internally, it will use something like std aligned storage and a bool to guard "have I been created"; or maybe a union. It may be possible for the compiler to prove the bool is not needed, but I doubt it.

An implementation can be downloaded from github or you can use boost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.