I was under the impression that static members of a class share their value between all instances of that class. My understanding seems to be lacking though, perhaps because of the inclusion of templates or constexpr in this example:
#include <iostream>
#include <array>
template <typename T, std::size_t maxSize>
class Foo
{
public:
unsigned int getLen() {
return containerLen;
}
private:
static constexpr std::size_t containerLen = maxSize + 1;
std::array<T, containerLen> arr;
};
int main()
{
Foo<int, 10> foo1;
std::cout << foo1.getLen() << std::endl;
Foo<int, 12> foo2;
std::cout << foo2.getLen() << std::endl;
std::cout << foo1.getLen() << std::endl;
return 0;
}
Which gives the following output:
11
13
11
I'm probably missing something obvious, but what is it that allows each instance of Foo to have a different value of containerLen?