Skip to content

Instantly share code, notes, and snippets.

@pixie-grasper
Created November 11, 2014 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixie-grasper/56ecefdc0d68b07844f7 to your computer and use it in GitHub Desktop.
Save pixie-grasper/56ecefdc0d68b07844f7 to your computer and use it in GitHub Desktop.
memo: nullptr confuses decltype(auto).
#include <stdio.h>
#include <malloc.h>
template <typename T>
auto my_alloc() -> decltype(auto) {
auto ret = static_cast<T*>(malloc(sizeof(T)));
if (ret == NULL) {
return nullptr;
}
return ret;
}
int main() {
auto a = my_alloc<int>();
free(a);
return 0;
}
@pixie-grasper
Copy link
Author

It's not compilable on my env. How about on yours?

I've checked on there, but get errored.
clang version 3.4.2 (tags/RELEASE_34/dot2-final)
Target: i386-pc-cygwin
Thread model: posix

@pixie-grasper
Copy link
Author

I've checked another env, failed.

$ clang++ 1.cc -std=c++1y
1.cc:10:3: error: 'decltype(auto)' in return type deduced as 'int *' here but
      deduced as 'nullptr_t' in earlier return statement
  return ret;
  ^
1.cc:14:12: note: in instantiation of function template specialization
      'my_alloc<int>' requested here
  auto a = my_alloc<int>();
           ^
1 error generated.

$ clang++ --version
clang version 3.5.0 (220663)
Target: x86_64-unknown-linux-gnu
Thread model: posix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment