|
C++ Template Metaprogramming
Solutions to the exercises throughout the book
|
Return different types based on parameter types. More...
Files | |
| file | exercise-4-5.hpp |
| Solution to Exercise 4-5. | |
Namespaces | |
| namespace | exercise_4_5 |
| Encapsulate solution for Exercise 4-5. | |
Functions | |
| static void | anonymous_namespace{chapter-4.cpp}::test_exercise_4_5 () |
| Tests for Exercise 4-5. | |
Return different types based on parameter types.
4-5. Consider the following function template, which is designed to provide a
"container-based" (as opposed to iterator-based) interface to std::find:
template <typename Container, typename Value>
typename Container::iterator
container_find(Container& c, Value const& v)
{
return std::find(c.begin(), c.end(), v);
}
As coded, container_find won't work for const containers; Container will
be deduced as const X for some container type X, but when we try to
convert the Container::const_iterator returned by std::find into a
Container::iterator, compilation will fail. Fix the problem using a
small metaprogram to compute container_find's return type.
|
static |
Tests for Exercise 4-5.
Definition at line 164 of file chapter-4.cpp.