4

In the following code, the compiler complains as: struct std::array<double,5ul> has no member named 'assign'. Here, it seems possible. Why is it so? (Compiler: g++ 4.8.2)

#include <array>

int main()
{
    std::array<double,5> arr;
    arr.assign(4.); // error: has no member named 'assign'
    return 0;
}
1
  • Are you asking why class template std::array has no assign member function? Commented Jun 17, 2014 at 5:47

4 Answers 4

14

array::assign() is a Visual Studio extension. You are compiling with g++. Check here standard g++ array

Sign up to request clarification or add additional context in comments.

3 Comments

s/standard g++/standard C++/
Why why? what is the need of this extension? .. +1 BTW
@Nawaz It helps stop the error message "has no member named 'assign'".
9

As already mentioned, there simply is no assign member to std::array.

The interesting question now becomes why ? After all, the other containers have an assign member method !

I would note that unlike other containers, std::array has a fixed size. If you use std::vector<T>::assign (which would be a close equivalent), the vector is resized appropriately to match the size of the sequence being assigned; with an array, however, that would be impossible:

  • what would you do if the sequence being assigned is shorter than the array ?
  • what would you do if the sequence being assigned is longer than the array ?

this would be counter-intuitive, as the question does not arise for the other containers since their size is just adapted on the fly.

For a similar reason, std::array does not have: reserve, capacity, clear, insert, emplace, erase, push/pop (and variants) or resize. All of them suppose a container which size may vary.

4 Comments

+1 Nice answer. In fact, you are answering the question I thought OP might have been asking.
@juanchopanza I must say that this is one VS extension that looks kinda handy though. I wasn't aware that it existed, but looking at it now, all it does is call fill_n to set all elements to the input argument.
@Praetorian But there's also std::array::fill for that.
@juanchopanza Haha, ok, never mind, back to being a silly extension then :)
5

That is very simple, as you can see here there is no assign member function for an std array. There is however a member function called fill you should be able to use.

Comments

2

The assign method it is not a member of std::array. The member fill does what the assign did in TR1.

assign was originally part of std::tr1::array (from the TR1) and was changed to be fill circa the C++0x (now C++11) draft n2798 (2008).

This is not really a Microsoft extension, I would imagine that they have probably maintained this method for compatibility with, and support for tr1 implementations in production (the method implementations are exactly the same).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.