Skip to content

Instantly share code, notes, and snippets.

@abrooks98
Last active Jun 18, 2018
Embed
What would you like to do?
Type deduction failure bug example
#include <vector>
#include <utility>
#include <stdio.h>
template<typename T>
class myvector : public std::vector<T> {};
template<typename T>
struct check_for
{
template<typename U>
static constexpr bool _data(U*, decltype(std::declval<U>().data())* = nullptr) { return true; }
template<typename U>
static constexpr bool _data(...) { return false; }
template<typename U>
static constexpr bool _get_allocator(U*, decltype(std::declval<U>().get_allocator())* = nullptr) { return true; }
template<typename U>
static constexpr bool _get_allocator(...) { return false; }
static constexpr bool data = _data<T>(nullptr);
static constexpr bool get_allocator = _get_allocator<T>(nullptr);
};
int main(int argc, char** argv)
{
printf("Check type 'bool' for function 'data': %d\n", check_for<bool>::data);
printf("Check type 'std::vector<int>' for function 'data': %d\n", check_for<std::vector<int>>::data);
printf("Check type 'myvector<int>' for function 'data': %d\n", check_for<myvector<int>>::data);
printf("Check type 'bool' for function 'get_allocator': %d\n", check_for<bool>::get_allocator);
printf("Check type 'std::vector<int>' for function 'get_allocator': %d\n", check_for<std::vector<int>>::get_allocator);
printf("Check type 'myvector<int>' for function 'get_allocator': %d\n", check_for<myvector<int>>::get_allocator);
decltype(std::declval<std::vector<int>>().get_allocator()) a;
int* b = a.allocate(10);
printf("%p\n", b);
a.deallocate(b, 10);
return 0;
}
@abrooks98

This comment has been minimized.

Copy link
Owner Author

@abrooks98 abrooks98 commented Jun 18, 2018

The output for check_for<std::vector<int>>::get_allocator (and possibly check_for<myvector<int>>>::get_allocator) should be 1. When running the example code with Intel C++ Compiler 18.0.1 20171018, there is type-deduction failure for _get_allocator, and the two commands output 0. This does not occur for GCC 4.9.3+ or Clang 3.90+

@abrooks98

This comment has been minimized.

Copy link
Owner Author

@abrooks98 abrooks98 commented Jun 18, 2018

This also occurs with ICC 18.0.2 20180210

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