STL vector as a function parameter with default value (heres another one for you Jerry...)
I have a fuction that takes a vector as a parameter. I want to be able to have a default value so I'm not required to pass anything if I don't need to. I pass vectors by reference because of some things I need to do with it that pointers don't do. anyway I can define my function like this func(vector<unsigned int> foo = vector<unsigned int>()); and everthing is fine, however if I define like this, func(vector<unsigned int>& foo = vector<unsigned int>()); I get error: invalid type `std::vector<unsigned int, std::allocator<unsigned int> >' for default argument to ` std::vector<unsigned int, std::allocator<unsigned int> >&' I am running SuSE 9.1 2.6.5-7.145-default This syntax works with Borland 6 windoze (disclaimer: no, not mine, co-worker's) the only work-around I've come up with is to declare a global variable vector<unsigned int> g_empty_vec; then use that func(vector<unsigned int>& foo = g_empty_vec); I'd like something a little cleaner if possible.... Thanks in advance, B-)
Brad, On Friday 11 February 2005 21:03, Brad Bourn wrote:
I have a fuction that takes a vector as a parameter.
I want to be able to have a default value so I'm not required to pass anything if I don't need to.
I pass vectors by reference because of some things I need to do with it that pointers don't do.
anyway
I can define my function like this
func(vector<unsigned int> foo = vector<unsigned int>());
and everthing is fine, however if I define like this,
func(vector<unsigned int>& foo = vector<unsigned int>());
I get
error: invalid type `std::vector<unsigned int, std::allocator<unsigned int> >' for default argument to ` std::vector<unsigned int, std::allocator<unsigned int> >&'
I am running SuSE 9.1 2.6.5-7.145-default
This syntax works with Borland 6 windoze (disclaimer: no, not mine, co-worker's) Easy to explain. The ability to pass temporaries to non-const reference
This is the same problem as you would get if you declared the function func(vector<unsigned int>& foo); and then called it with func(vector<unsigned int>()) This is an error because 'vector<unsigned int>()' is a temporary and you can only pass temporaries to 'const' reference parameters. Your function must be declared func(const vector<unsigned int>& foo); OR func(vector<unsigned int> foo); to work with temporaries. parameters has been disallowed for years in standard C++. It is incredibly dangerours. Some compilers support it despite this! Michael -- ___________________________________ Michael Stevens Systems Engineering Navigation Systems, Estimation and Bayesian Filtering http://bayesclasses.sf.net ___________________________________
That did the trick! thank you very much. B-) On Saturday 12 February 2005 03:52 am, Michael Stevens wrote:
Brad,
On Friday 11 February 2005 21:03, Brad Bourn wrote:
I have a fuction that takes a vector as a parameter.
I want to be able to have a default value so I'm not required to pass anything if I don't need to.
I pass vectors by reference because of some things I need to do with it that pointers don't do.
anyway
I can define my function like this
func(vector<unsigned int> foo = vector<unsigned int>());
and everthing is fine, however if I define like this,
func(vector<unsigned int>& foo = vector<unsigned int>());
This is the same problem as you would get if you declared the function func(vector<unsigned int>& foo); and then called it with func(vector<unsigned int>())
This is an error because 'vector<unsigned int>()' is a temporary and you can only pass temporaries to 'const' reference parameters. Your function must be declared func(const vector<unsigned int>& foo); OR func(vector<unsigned int> foo); to work with temporaries.
I get
error: invalid type `std::vector<unsigned int, std::allocator<unsigned int> >' for default argument to ` std::vector<unsigned int, std::allocator<unsigned int> >&'
I am running SuSE 9.1 2.6.5-7.145-default
This syntax works with Borland 6 windoze (disclaimer: no, not mine, co-worker's)
Easy to explain. The ability to pass temporaries to non-const reference parameters has been disallowed for years in standard C++. It is incredibly dangerours. Some compilers support it despite this!
Michael
-- ___________________________________ Michael Stevens Systems Engineering
Navigation Systems, Estimation and Bayesian Filtering http://bayesclasses.sf.net ___________________________________
On Saturday 12 February 2005 03:52 am, Michael Stevens wrote:
This is an error because 'vector<unsigned int>()' is a temporary and you can only pass temporaries to 'const' reference parameters. Your function must be declared func(const vector<unsigned int>& foo); OR func(vector<unsigned int> foo); to work with temporaries.
This does allow it to compile, however, once inside the function, I can't use the std::find function because STL doesn't seem to like const iterators... here is the error I get /usr/include/g++/bits/stl_iterator.h: In constructor ` __gnu_cxx::__normal_iterator<_Iterator, _Container>::__normal_iterator(const __gnu_cxx::__normal_iterator<_Iter, _Container>&) [with _Iter = const unsigned int*, _Iterator = unsigned int*, _Container = std::vector<unsigned int, std::allocator<unsigned int> >]': /usr/include/g++/bits/stl_iterator.h:598: error: invalid conversion from `const unsigned int* const' to `unsigned int*' B-)
participants (2)
-
Brad Bourn
-
Michael Stevens