Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <type_traits>
- #include <memory>
- struct dummy {};
- template<class T> class Vector {
- T arr[42];
- template<class X = void> inline void push_back_impl(const dummy &x) {}
- template<class X = void> inline void push_back_impl(const T &x) {
- //copy assignment is used here
- arr[0] = x;
- }
- public:
- void push_back(typename std::conditional<std::is_copy_assignable<T>::value, const T&, dummy>::type x) {
- push_back_impl(x);
- }
- };
- //explicit instantiations work properly
- template class Vector<int>;
- template class Vector<std::unique_ptr<int>>;
- int main() {
- //this is OK:
- Vector<int> ww;
- ww.push_back(5);
- Vector<std::unique_ptr<int>> vv;
- //uncomment to get compile error:
- // vv.push_back(std::unique_ptr<int>());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement