Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How/What to return in this case?

CODE:

vector<DWORD> get_running_proc_list()
{
    DWORD proc_list[1024], size;

    if(!EnumProcesses(proc_list, sizeof(proc_list), &size))
    {
        return 0; // PROBLEM HERE!! 
    }

    vector<DWORD> _procs(proc_list, proc_list + size/sizeof(DWORD));
    return _procs;
}

ERROR:

cannot convert from 'int' to 'const std::vector<_Ty>'

What is the best possible way to fix this error?
Is there a better way than just returning an empty vector?

like image 276
Pratik Deoghare Avatar asked Jun 23 '11 08:06

Pratik Deoghare


People also ask

How do you say return an item?

Smile and say, "Hi, I want to return this item which I bought last week." Show the clerk the item and your receipt. Don't delay returning the item. Some stores allow returns but only for a certain amount of time. For example, many stores only allow returns within 14 days of the purchase.

What is a good reason to return an item?

The product was damaged or defective Items sometimes get damaged during shipping, or they leave the warehouse in damaged or defective condition. Once again, the merchant is obligated to issue a refund. The customer clearly has the right to receive an undamaged and functional product or get their money back.


6 Answers

Well, your function returns vector, not DWORD. Can't you return just an empty vector:

return std::vector< DWORD >();

or

return std::vector< DWORD >( 1, 0 );

If you really need the 0?


EDIT:

There's another option, if empty vector is not a solution (in case, that it's some kind of valid value and you need to know) - use exception. You can make your own class for exception or use some standard one. So, you can do it like this:

if(!EnumProcesses(proc_list, sizeof(proc_list), &size))
{
   throw MyException( "some message, if you want" );
}

I'd advice, if you choose this option, to inherit std::exception.

Or, you can return pointer to std::vector and return NULL in this case. But I would not recommend this. But it's just my opinion.

like image 126
Kiril Kirov Avatar answered Sep 21 '22 01:09

Kiril Kirov


Throw an exception. That's what they're for. After all, there definitely should be a list of processes that are running.

And definitely do not fail to return by value or something. In this case, the RVO/NRVO is trivially applied. I mean, at worst, you could swaptimize.

like image 35
Puppy Avatar answered Sep 22 '22 01:09

Puppy


Edit: (after reading edited version of question)

Alternatives:

  1. You can make your function to return void and pass to it a vector by reference (or by pointer) then fill the vector in function body.
  2. You can make your function to return boost::shared_ptr<vector<DWORD> > (or some other smart pointer), construct and fill the vector in function body (of course in dynamic memory), then return it's address or NULL.
  3. Throw an exception if above solutions are not suitable.
like image 36
Mihran Hovsepyan Avatar answered Sep 21 '22 01:09

Mihran Hovsepyan


if(!EnumProcesses(proc_list, sizeof(proc_list), &size))
{
  vector<DWORD> empty;       
  return empty;  <--- 0 sized vector
}

You can return an empty vector<>.

As a side note, I would not recommend to return vector by value. Instead pass the vector<> as parameter to be assured that unnecessary copies will not happen.

void get_running_proc_list(vector<DWORD> &_procs) pass by reference and populate
{
...
}
like image 40
iammilind Avatar answered Sep 20 '22 01:09

iammilind


Replace

return 0; // PROBLEM HERE!!  

with

return vector<DWORD>(); // NO PROBLEM!!  
like image 34
Alexey Malistov Avatar answered Sep 19 '22 01:09

Alexey Malistov


How about a boost::optional? It adds pointer semantics to normal objects and allows them to be either set or not, without dynamic allocation.

#include <boost/optional.hpp>

typedef boost::optional<std::vector<DWORD>> vec_opt;

vec_opt get_running_proc_list()
{
    DWORD proc_list[1024], size;

    if(!EnumProcesses(proc_list, sizeof(proc_list), &size))
    {
        return 0; 
    }

    vector<DWORD> _procs(proc_list, proc_list + size/sizeof(DWORD));
    return _procs;
}

And that's all you need to do, just change the return type. On the calling site:

vec_opt v = get_running_proc_list();
if(v){
  // successful and you can now go through the vector, accessing it with *v
  vector<DWORD>& the_v = *v;
  // use the_v ...
}
like image 28
Xeo Avatar answered Sep 20 '22 01:09

Xeo