I had the following problem:
I have a generic container that can perform some type operations. Operations for simplicity, thread safe, on request. And , requested , means that the type in the container has typedef std::true_type needs_thread_safety; .
struct thread_safe_item { typedef std::true_type needs_thread_safety; }; struct thread_unsafe_item { typedef std::false_type needs_thread_safety; }; template<typename TItem> container { };
But I want needs_thread_safety be enabled and not need to be defined (= default false_type). I tried the following:
struct thread_unsafe_item { }; template<typename TItem> struct thread_safety_selector { template<typename T> struct has_defined_thread_safety { typedef char yes[1]; typedef char no[2]; template <typename C> static yes& test(typename C::needs_thread_safety*); template <typename> static no& test(...); static const bool value = sizeof(test<T>(0)) == sizeof(yes); }; typedef typename std::conditional< has_defined_thread_safety<TItem>::value, typename TItem::needs_thread_safety, std::false_type >::type needs_thread_safety; }; .... struct <typename TItem> container { };
But, apparently, no lazy evaluation occurs, since error error C2039: 'needs_thread_safety' : is not a member of 'thread_unsafe_item' .
How can I get the default value for an argument not specified?
This is for educational purposes, so I do not need another way to solve this problem.
Thanks!
source share