1

For example

bool read(Input &input);

Input input; //error
bool success = read(input);

will be an error because Input has no default constructor.

Is there any trickery I can use to get the Input object out of the function in this case? I imagine there must be some unique_ptr trickery available to me, but I'm not sure exactly how. Feel free to suggest other methods.

Please suggest with example how the read function could look.

I would rather not create a (meaningless) default constructor for Input just for this purpose, and note that this is just a silly example, so don't attach any special meaning to the words "Input", "read", etc. :)

qPCR4vir
  • 3,521
  • 1
  • 22
  • 32
user2015453
  • 4,844
  • 5
  • 25
  • 27

4 Answers4

1
bool read(unique_ptr<Input> &input)  // read asume input is disposable/empty
{    ....
  input.reset(new Input( a,d,c ) );
     ....
 }

....
unique_ptr<Input> input;      //error ?
bool success = read(input);
if (input)
  if (succes)
     input->X();
  else
     input->Y();
qPCR4vir
  • 3,521
  • 1
  • 22
  • 32
  • Yes I imagine something like this, but the problem is that you can't reassign to the input pointer within the read function. I guess I could do "input.reset(new whatever)", but does that sound right? – user2015453 Feb 21 '13 at 14:20
  • A `unique_ptr` is enough in itself to signal "no object", so i do not see the purpose in passing it by reference with a `bool` function result. Also, dynamic allocation is, relative to ordinary C++ stack allocation, extremely inefficient. So what compelling argument is there for taking on that verbosity, obscurity and inefficiency? – Cheers and hth. - Alf Feb 21 '13 at 14:28
  • @ Cheers and hth. - Alf : this is just a silly example, so don't attach any special meaning to the words "Input", "read", etc. ... Because in my case, the return type will be important, and even "false" is not invalid – qPCR4vir Feb 21 '13 at 14:36
0

From the comments, it appears that your problem is to design a function that

  • can fail (and if so should signal that to caller),

  • but if not, produces a value of a type without a default cconstructor

The first point is easy: use exceptions.

The second point is also easy: use the function return value feature.

I.e.,

Object foo()
{
    if( "didna wrok" )
    {
        throw std::runtime_error( "foo: I failed, miserably" );
    }

    return Object( arg1, arg2, arg3 );
}

Now there also many other ways to do this, but the above is the most natural, directly using the language features that are intended to support and fully solve these aspects.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0
unique_ptr<Input> input_ptr = read();

where read() is defined as:

unique_ptr<Input> read()
{
    .
    .
    .
    return unique_ptr<Input>(new Input(x,y,z));
}
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • A more efficient and safe variant is to use an implementation of Barton and Nackman's `Fallible` class, such as `boost::optional`. It's also trivial to implement (in a few minutes) for POD inner type. I didn't suggest that in my answer -- I merely stated that there are many more possibilities -- because evidently the OP is learning the basics of C++. – Cheers and hth. - Alf Feb 21 '13 at 14:25
0

If you are in pre-C+11 world, there is a workaround by use of malloc:

bool read(Input &input); // keep the function read intact 

Input* input = static_cast<Input*>(malloc(sizeof(Input))); // bypass constructor
bool success = read(*input);
...
free(input); // don't forget to free input later
Hui Zheng
  • 10,084
  • 2
  • 35
  • 40