I have a templated Container storing values in vector. There is no problem with strings or ints, but size_t values don't compile. It seems the compiler assumes I want to initialize the vector with ints instead of size_t values. How can I solve this problem?
#include <iostream>
#include <vector>
template <class ValueType>
class Container {
public:
template <typename... ValueTypes>
Container(ValueTypes&&... values) :
currentIndex(0),
values{ std::forward<ValueTypes>(values)... }
{}
const ValueType operator()() const {
return values[currentIndex];
}
protected:
size_t currentIndex;
std::vector<ValueType> values;
};
int main() {
Container<std::string> container1("10", "20", "30"); // ok
Container<int> container2(10, 20, 30); // ok
Container<size_t> container3(10, 20, 30); // compile error, assumed ints!
}
10,20and30are indeedintliterals, so the narrowing conversion error/warning is expected.You can use a
u(orULL) suffix to make them literals compliant withsize_t(which is unsigned):