declval

Does T have to be a complete type to be used in `std::declval<T>`?

こ雲淡風輕ζ 提交于 2020-01-03 07:59:10
问题 Consider this example (coming from here): #include <type_traits> #include <iostream> template <typename U> struct A { }; struct B { template <typename F = int> A<F> f() { return A<F>{}; } using default_return_type = decltype(std::declval<B>().f()); }; int main() { B::default_return_type x{}; std::cout << std::is_same< B::default_return_type, A<int>>::value; } It compiles with no errors on gcc9.2 but gcc7.2 and clang 10.0.0 complain about B not being complete. Clangs error is: prog.cc:11:58:

Is There a declval for Function Pointers?

泄露秘密 提交于 2019-12-11 17:51:33
问题 I have a function and I need to test whether I can pass an argument of a given type to it. For example: template<typename T, auto F> decltype(F(declval<T>{})) foo(); Calling foo<int, bar>() does 2 things: Sets the return type of foo would have the same return type as bar Ensures that bar is a function that accepts an argument of type T Unfortunately I don't have access to auto template types, but I still want to accomplish both of these. What I need is a decltype for function pointers, which

How do we test if an expression of a certain type can be invoked with a prvalue?

泪湿孤枕 提交于 2019-12-03 11:35:39
问题 With c++17 we have fancy new is_invocable and fancy new prvalues that aren't really values. This permits you to create an object without having to first logically construct it, then elide the construction. I have run into a problem where using std::is_invocable to test if you can call something, and prvalue rules, seem to collide: struct no_move { no_move(no_move&&)=delete; explicit no_move(int) {} }; void f( no_move ) {} now can we ask if f can be invoked using a prvalue of type no_move ? f(