Reputation: 6852
when I try to use an initializer list for a member that contains references, I get the following error:
no matching function for call to ‘std::vector<const Exp&>::vector(<brace-enclosed initializer list>)’
I have read several related posts, but first, they seem to get a different error; second, they qualify the use of references "as pointless".
Without getting into philosophical discussions, indeed I would appreciate knowing if it is possible to make the below example work:
#include <vector>
class Exp {
};
class Integer : public Exp {
public:
const int value;
Integer(const int val) : value(val) { }
};
int main() {
const auto a1 = Integer(1);
const auto a2 = Integer(2);
const std::vector<const Exp&> va{a1,a2};
}
Could it be a missing constructor for the vector
class? Thanks a lot!
gcc (Ubuntu 5.3.1-14ubuntu2.1) 5.3.1 20160413
[Edited to remove spurious example]
Upvotes: 2
Views: 297
Reputation: 6852
Thanks to everybody!!! I've settled on this solution for now:
std::array<const std::reference_wrapper<const Exp>, 2> ae{a1,a2};
I need to investigate more but I think that will do what I want for now.
Upvotes: 1
Reputation: 119069
Although this is not explicitly stated in the standard, attempting to use standard library containers to store non-object types should be regarded as undefined behaviour. See [container.requirements.general],
p1: "Containers are objects that store other objects..."
p4: "... X
denotes a container class containing objects of type T
..."
and so on.
Upvotes: 3