c++ - Clang 3.5: Detecting a gobal function doesn't exist using SFINAE -


so have following piece of code works under vs2012. detects whether global function 'bar' exists.

sadly fails compile under clang 3.5 error:

error : use of undeclared identifier 'bar'   template <typename t> static int foo( decltype( bar )* ) { return 2; }                                                               ^  

does clang support kind of thing , if correct syntax?

thanks

template <typename t> static int foo( decltype( bar )* ) { return 2; }       template <typename t> static int foo(...) { return 1; }   void main() {     printf("%d", foo<int>(nullptr)); } 

if know exact signature of function looking for, can solved sfinae (tested clang 3.6 , gcc 4.9):

#include <stdio.h>  int somemethod(double d) {         // nop         return 0; }  struct hassomemethod {         typedef char no[2];          template <typename c>         static auto test(c c) -> decltype(somemethod(c));         //static auto test(c c) -> decltype(someothermethod(c));          template <typename>         static no& test(...);          static const bool value = sizeof(test<double>(1.0)) == sizeof(int); };  int main() {     printf("%d\n", hassomemethod::value); } 

this outputs 1, , 0 if switch test method commented version.

it works global method long has @ least 1 parameter can extract template parameter.

sfinae abbreviation "substitution failure not error". means, have to:

  • use template parameter substituted compiler
  • your subject must somehow depend on template parameter

my example solves latter providing templated parameter subject function - if there no such function, can't substituted, valid.

same result if there no such overload looking for. can change parameter of somemethod in code, or @ testing point, , you'll see.

unfortunately, case using type_traits, , similar tricks isn't possible. example, following method:

template<typename c> static auto test(c c) -> typename std::is_same<decltype(somexxmethod()), decltype(c)>::type 

has same problem original code. member detection works because c::somemethod depends on template parameter.

because of requirement, method no parameter, non-void return type can checked, have no idea how right now.

but if have void method without parameters, i'm sure won't find standard way - can't find valid type specification has both type , method in it.

clang specific

if want clang specific check, there clang specific extensions

for example, can use #if __is_identifier(somemethod) macro, checks if given identifier exists. might variable, or function, or anything, inside block, can use sfinae decide it's type.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -