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 way to fix this error?
Is there a better way than just returning an empty vector?

+5
source share
7 answers

Well, your function returns vector, not DWORD. You cannot return an empty vector:

return std::vector< DWORD >();

or

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

If you really need one 0?


EDIT

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

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

, , std::exception.

std::vector NULL . . .

+4

. . , .

- . RVO/NRVO . , swaptimize.

+3

: ( )

:

  • void ( ), .
  • boost::shared_ptr<vector<DWORD> > ( - ), ( ), NULL.
  • , .
+2
if(!EnumProcesses(proc_list, sizeof(proc_list), &size))
{
  vector<DWORD> empty;       
  return empty;  <--- 0 sized vector
}

vector<>.

vector . vector<> as, , .

void get_running_proc_list(vector<DWORD> &_procs) pass by reference and populate
{
...
}
+1

return 0; // PROBLEM HERE!!  

return vector<DWORD>(); // NO PROBLEM!!  
+1

boost::optional? , .

#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 all you have to do is 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 ...
}
+1
source

You are trying to return two logically different bits of information: first, "What is the list of processes?" and secondly, "Is it possible to compute a list of processes?" I suggest you return those of two variable variables:

// UNTESTED
bool get_running_proc_list(vector<DWORD>& result)
{
  DWORD proc_list[1024], size;

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

  result = vector<DWORD>(proc_list, proc_list + size/sizeof(DWORD));
  return true;
}

But I can try to save a couple of memcpy's:

// UNTESTED
bool get_running_proc_list(vector<DWORD>& result)
{
  result.clear();
  result.resize(1024);

  DWORD size;
  if(!EnumProcesses(&result[0], result.size()*sizeof(DWORD), &size))
  {
    result.clear();
    return false; 
  }
  result.resize(size/sizeof(DWORD));

  return true;
}
0
source

All Articles