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?
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.
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.
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.
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.
Edit: (after reading edited version of question)
Alternatives:
void
and pass to it a vector by reference (or by pointer) then fill the vector in function body.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.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
{
...
}
Replace
return 0; // PROBLEM HERE!!
with
return vector<DWORD>(); // NO PROBLEM!!
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 ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With