• Forum
  • Lounge
  • Defect of the C++ Standard relative to c

 
Defect of the C++ Standard relative to class std::basic_string

Class std::basic string has the following constructor

basic_string(const basic_string& str, size_type pos, size_type n = npos,
const Allocator& a = Allocator());

but the corresponding assign method is declared as

basic_string& assign(const basic_string& str, size_type pos, size_type n);

I do not see the reason why the third parameter may not have a default argument the same way as the corresponding constructor. That is IMO the assign method should be declared as

basic_string& assign(const basic_string& str, size_type pos, size_type n = npos ) ;
closed account (o1vk4iN6)
Seems they are moving away from default arguments in member functions ? At least it's been removed in some cases like vector::resize().

http://en.cppreference.com/w/cpp/container/vector/resize
As for the resize the situation is different. Now there are two overloaded functions.

That is one function with the default argument

void resize( size_type count, T value = T() ); (until C++11)

was substituted for two overloaded functions.

void resize( size_type count ); (since C++11)

void resize( size_type count, const value_type& value); (since C++11)

For the member function assign of std::basic_string there is no such a substitution. If you see the definition of the class you will see that for each constructor except the explicit constructor there is a corresponding member function assign. So there is relation one to one.
closed account (o1vk4iN6)
Well they would only need to add:

basic_string& assign(const basic_string& str, size_type pos)

But yah it is strange that they don't provide it when the constructor does.
No they need not to provide one more overloaded function. They should update the C++ Standard and specify the default argument for the function.

Take into account that situation is different compared to the resize.

size_type is a typedef of a fundamental type. While as for the resize the old realization accepts the argument by value but the new realization accepts the argument by const reference. So there was a sense to substitute the old realization for two new overloaded functions. And there is no any sense to do the same with the assign.
Last edited on
Topic archived. No new replies allowed.