I need a portable way to define a template class that checks the validity of some expression in its parameter. Ideally, it should work the same in MSVC 2013+, Clang 3.1+ and GCC 4.8+.
Usage example:
struct MyStruct
{
int Back() {return 5;}
};
static_assert(HasBack<MyStruct>::value, "Back must exist!");
I tried this code:
template<typename T, typename dummy=void> struct HasBack: std::false_type {};
template<typename T> struct HasBack<T, void_t<decltype(declval<T>().Back())>>: std::true_type {};
It works in clang, but does not work in Visual Studio. Especially for him, I wrote another implementation using compiler extensions:
template<typename T> struct HasBack
{
__if_exists(void_t<decltype(declval<T>().Back())>) {enum {value=1};}
__if_not_exists(void_t<decltype(declval<T>().Back())>) {enum {value=0};}
};
It compiles and works in Visual Studio 2013+, but completely disables IntelliSense in any project that includes this code. Are there any workarounds for these problems, or maybe there is another way to perform expression checking that works for all compilers?