CS253

CS253: Software Development with C++

Spring 2018

Duck Typing

See this page as a slide show

Duck Typing

CS253 Duck Typing

Rules for Rulers

Rules for Containers

Enforcement

Example

A vector requires that its stored type be copyable, both via copy ctor and assignment operator. For example, you can have a vector<string>, because strings are copyable

string now = "20180122T170159";
vector<string> v = {now, now, now};
cout << v.size() << '\n';
3

Example

How about a non-copyable class?

class Mom {
  public:
    Mom() = default;                        // I like the default ctor.
    Mom(const Mom &) = delete;              // You can’t change Mom!
    Mom &operator=(const Mom &) = delete;   // You can’t change Mom!
};

int main() {
    Mom helen;
    vector<Mom> v = {helen, helen, helen};
}
c.cc: In function 'int main()':
c.cc:10:41: error: use of deleted function 'Mom::Mom(const Mom&)'
c.cc:4:5: note: declared here
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/vector:64:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/random.h:34,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/random:49,
                 from c.cc:10:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_vector.h:375:7: note:   
   initializing argument 1 of 'std::vector<_Tp, 
   _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp 
   = Mom; _Alloc = std::allocator<Mom>; std::vector<_Tp, 
   _Alloc>::allocator_type = std::allocator<Mom>]'
c.cc:10:41: error: use of deleted function 'Mom::Mom(const Mom&)'
c.cc:4:5: note: declared here
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/vector:64:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/random.h:34,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/random:49,
                 from c.cc:10:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_vector.h:375:7: note:   
   initializing argument 1 of 'std::vector<_Tp, 
   _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp 
   = Mom; _Alloc = std::allocator<Mom>; std::vector<_Tp, 
   _Alloc>::allocator_type = std::allocator<Mom>]'
c.cc:10:41: error: use of deleted function 'Mom::Mom(const Mom&)'
c.cc:4:5: note: declared here
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/vector:64:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/random.h:34,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/random:49,
                 from c.cc:10:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_vector.h:375:7: note:   
   initializing argument 1 of 'std::vector<_Tp, 
   _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp 
   = Mom; _Alloc = std::allocator<Mom>; std::vector<_Tp, 
   _Alloc>::allocator_type = std::allocator<Mom>]'
c.cc:10:41: error: use of deleted function 'Mom::Mom(const Mom&)'
c.cc:4:5: note: declared here
c.cc:10:41: error: use of deleted function 'Mom::Mom(const Mom&)'
c.cc:4:5: note: declared here
c.cc:10:41: error: use of deleted function 'Mom::Mom(const Mom&)'
c.cc:4:5: note: declared here
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_tempbuf.h:60:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_algo.h:62,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/algorithm:62,
                 from c.cc:1:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = Mom; _Args = {const Mom&}]':
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_uninitialized.h:75:18:   required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const Mom*; _ForwardIterator = Mom*; bool _TrivialValueTypes = false]'
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_uninitialized.h:126:15:   required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const Mom*; _ForwardIterator = Mom*]'
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_uninitialized.h:281:37:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = const Mom*; _ForwardIterator = Mom*; _Tp = Mom]'
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_vector.h:1288:33:   required from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const Mom*; _Tp = Mom; _Alloc = std::allocator<Mom>]'
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_vector.h:379:2:   required from 'std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = Mom; _Alloc = std::allocator<Mom>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Mom>]'
c.cc:10:41:   required from here
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_construct.h:75:7: error: use of 
   deleted function 'Mom::Mom(const Mom&)'
c.cc:4:5: note: declared here

How enforcement “works”

Modified: 2017-05-09T21:25

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building