CS253: Software Development with C++

Spring 2018

Duck Typing

See this page as a slide show

CS253 Duck Typing

🦆 🦆 🦆 The Saying 🦆 🦆 🦆

🦆

🙶If it walks like a duck,
  and quacks like a duck,
  then it’s a duck.🙷

This is an old common-sense saying that stresses functionality over formal definition. If I were doing 200mph on I-25 in Luke Skywalker’s speeder, I wouldn’t get far claiming that it’s “not a car”. The law would probably view it as a car, even though it has no wheels.

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 = "2018-04-25T02:50:10"; // ISO 8601, baby!
vector<string> v;
v.push_back(now);
cout << v.front() << '\n';
2018-04-25T02:50:10

Example

How about a non-copyable class?

vector<istream> v;
cout << "size=" << v.size() << endl;
size=0
vector<istream> v;
v.push_back(cin);
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/x86_64-pc-linux-gnu/bits/c++allocator.h:33:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/allocator.h:46,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/string:41,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/locale_classes.h:40,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/ios_base.h:41,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:40,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/ext/new_allocator.h: In instantiation of 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::basic_istream<char>; _Args = {const std::basic_istream<char, std::char_traits<char> >&}; _Tp = std::basic_istream<char>]':
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/alloc_traits.h:455:4:   required from 'static void std::allocator_traits<std::allocator<_Tp> >::construct(std::allocator_traits<std::allocator<_Tp> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::basic_istream<char>; _Args = {const std::basic_istream<char, std::char_traits<char> >&}; _Tp = std::basic_istream<char>; std::allocator_traits<std::allocator<_Tp> >::allocator_type = std::allocator<std::basic_istream<char> >]'
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_vector.h:918:30:   required from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::basic_istream<char>; _Alloc = std::allocator<std::basic_istream<char> >; std::vector<_Tp, _Alloc>::value_type = std::basic_istream<char>]'
c.cc:2:16:   required from here
/usr/local/gcc-6.3.0/include/c++/6.3.0/ext/new_allocator.h:120:4: error: 'std::basic_istream<_CharT,
   _Traits>::basic_istream(const std::basic_istream<_CharT, _Traits>&) [with 
   _CharT = char; _Traits = std::char_traits<char>]' is protected within this 
   context
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/sstream:38:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/quoted_string.h:38,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:45,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/istream:611:7: note: declared protected 
   here
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/x86_64-pc-linux-gnu/bits/c++allocator.h:33:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/allocator.h:46,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/string:41,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/locale_classes.h:40,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/ios_base.h:41,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:40,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/ext/new_allocator.h:120:4: error: use of 
   deleted function 'std::basic_istream<_CharT, _Traits>::basic_istream(const 
   std::basic_istream<_CharT, _Traits>&) [with _CharT = char; _Traits = 
   std::char_traits<char>]'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/sstream:38:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/quoted_string.h:38,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:45,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/istream:611:7: 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 = std::basic_istream<char>; _Args = {std::basic_istream<char, std::char_traits<char> >}]':
/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 = std::move_iterator<std::basic_istream<char>*>; _ForwardIterator = std::basic_istream<char>*; 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 = std::move_iterator<std::basic_istream<char>*>; _ForwardIterator = std::basic_istream<char>*]'
/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 = std::move_iterator<std::basic_istream<char>*>; _ForwardIterator = std::basic_istream<char>*; _Tp = std::basic_istream<char>]'
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_uninitialized.h:303:2:   required from '_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = std::basic_istream<char>*; _ForwardIterator = std::basic_istream<char>*; _Allocator = std::allocator<std::basic_istream<char> >]'
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/vector.tcc:422:8:   required from 'void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {const std::basic_istream<char, std::char_traits<char> >&}; _Tp = std::basic_istream<char>; _Alloc = std::allocator<std::basic_istream<char> >]'
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_vector.h:924:4:   required from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::basic_istream<char>; _Alloc = std::allocator<std::basic_istream<char> >; std::vector<_Tp, _Alloc>::value_type = std::basic_istream<char>]'
c.cc:2:16:   required from here
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_construct.h:75:7: error: 'std::basic_istream<_CharT,
   _Traits>::basic_istream(std::basic_istream<_CharT, _Traits>&&) [with _CharT 
   = char; _Traits = std::char_traits<char>]' is protected within this context
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/sstream:38:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/quoted_string.h:38,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:45,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/istream:613:7: note: declared protected 
   here

How enforcement “works”

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-24T16:51

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building