I have a template class that can (and sometimes has to) take a const type, but there is a method that returns a new instance of the class with the same type, but should be explicitly non-const. For example, the following code fails to compile
template<class T> class SomeClass {
public:
T val;
SomeClass(T val) : val(val) {}
SomeClass<T> other() {
return SomeClass<T>(val);
}
};
int main() {
SomeClass<const int> x(5);
SomeClass<int> y = x.other();
return 0;
}
because even though there’s a copy on val during the constructor, it’s copying to the same type – const int. Just like you can distinguish between T and const T in a template, is there a way to distinguish between T and “nonconst T“?
std::remove_constis from<type_traits>and is C++11. There’s probably aboost::remove_constin Boost.TypeTraits, or you can even roll your own. It’s also possible to usestd::remove_cv.