Idiom detection with failing static_assert function

Is there a way to use the discovery identifier (or another method) to check if the function is valid for the given template arguments, if that failed due to static_assert?

The example below shows that confidence foo(failure to evaluate return type) is determined by purpose, but not for bar(failure static_assert).

#include <iostream>
#include <type_traits>

template <typename... T> using void_t = void;

template <class AlwaysVoid, template<class...> class Op, class... Args>
struct detector: std::false_type { };

template <template<class...> class Op, class... Args>
struct detector<void_t<Op<Args...>>, Op, Args...>: std::true_type { };

template <template<class...> class Op, class... Args>
constexpr bool is_detected = detector<void, Op, Args...>::value;

template <typename T>
std::enable_if_t<!std::is_void<T>::value> foo() {
  std::cout << "foo" << std::endl;
}

template <typename T>
void bar() {
  static_assert( !std::is_void<T>::value );
  std::cout << "bar" << std::endl;
}

template <typename T> using foo_t = decltype(foo<T>());
template <typename T> using bar_t = decltype(bar<T>());

int main(int argc, char* argv[]) {

  foo<int>();
  // foo<void>(); // fails as expected

  bar<int>();
  // bar<void>(); // fails as expected

  std::cout << std::boolalpha;

  // detection works for foo
  std::cout << is_detected<foo_t,int > << std::endl; // true
  std::cout << is_detected<foo_t,void> << std::endl; // false

  // but not for bar
  std::cout << is_detected<bar_t,int > << std::endl; // true
  std::cout << is_detected<bar_t,void> << std::endl; // true !!!
}

It is for this reason that I can not determine whether boost::lexical_castfor the given types.

+6
source share
2 answers

SFINAE , SFINAE , .

bar void(void), SFINAE.

( ) :

template <typename T> 
using CanCallFoo_t = decltype(&foo<T>);

template<class T>
using CanCallFoo = detect<T, CanCallFoo_t, void>;

template<class T>
using CanCallBar_t = decltype(&bar<T>);

template< class T>
using
CanCallBar = detect<T, CanCallBar_t, void>;

//...
std::cout << CanCallFoo<int>::value << std::endl; // true
std::cout << CanCallFoo<void>::value << std::endl; // false

std::cout << CanCallBar<int>::value << std::endl;
std::cout << CanCallBar<void>::value << std::endl;

, SFINAE , .

:
   static_assert( !std::is_void<T>::value );

, foo, foo SFINAE void

static_assert , , SFINAE.

+6

, , static_assert sfinae. , , .

, , lexical_cast . lexical_cast lexical_cast. , , , , ().

, , , , .

+1

All Articles