The "four basic kinds" of C++ sequence containers
(vector
, forward_list
, list
, and deque
)
all have an assign
method. For example, std::vector
has
(quoting cppreference.com):
void assign( size_type count, const T& value );
template< class InputIt >
void assign( InputIt first, InputIt last );
void assign( std::initializer_list<T> ilist );
These all have semantics that are, as far as I can tell, the same as
using operator=
on a temporary constructed using the arguments to the
assign
method. Why have a separate method to do, for example:
vec.assign({1,2,3});
instead of just:
vec = {1,2,3};
?
Meanwhile, the C++ associative containers such as std::map
do not have
an assign
method. Why not? What reason is sufficiently compelling
for the sequence containers to have it but not associative containers?
In particular, my initial guess for why sequence containers have
assign
is it avoids creating a temporary in the applicable
circumstance, but the same reasoning would apply to the associative
containers, right? (And with move assignment, creating the temporary is
probably insignificant anyway.)
This question has a practical application: I am creating a container
that has characteristics of both a sequence and associative container,
and wondering if there is a good reason to supply an assign
method for
it.
Some have claimed that this question is opinion-based, and hence inappropriate to ask on this site, but I think that is incorrect. I am not asking what the reader would do when designing a language, I am asking for the rationale used by the C++ language designers when designing the C++ language. This has an objectively correct answer: this decision was made by specific people for specific reasons. The question seeks those reasons.
There are several fact-based ways to answer a question like this:
There could be a clear, convincing technical basis for the decision, such that almost any competent designer would make the same choice. In this case, one needs only show a significant (say) performance advantage to having
assign
for sequence containers and a corresponding lack thereof for associative containers. These performance characteristics are not opinions. The highly-upvoted answer by JaMiT took this approach. (Whether a claimed technical basis is "convincing" is itself subjective, but that is a property of the answer, not the question, and even if one technical argument is found lacking, that does not exclude the possibility of a stronger one that hasn't yet been articulated.)There could be a contemporaneous rationale document. I wasn't able to find one addressing this specific question, but that does not mean it does not exist. Any such document would definitively answer the question, reinforcing the claim that this question has an objectively correct answer, even if it may be challenging to discover it.
Similarly, one of the people involved in or witness to the decision could provide an answer based on that personal experience. In that case, one could potentially question the credibility of an answer, but that doesn't make it an opinion.
As is often the case with language features, there could be a pre-standardization proposal, and associated revision history of that proposal, from which a likely motivation can be at least inferred. Informed speculation based on such a proposal is not the same as an opinion, although speculation does involve an element of subjective judgment. In my answer, I took this approach, treating STL as the pre-standardization proposal.
std::map::assign
was not even offered and discussed.