Questions tagged [declval]

29 questions
51
votes
3 answers

Why does std::declval add a reference?

std::declval is a compile-time utility used to construct an expression for the purpose of determining its type. It is defined like this: template< class T > typename std::add_rvalue_reference::type declval() noexcept; Would this not be simpler…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
50
votes
2 answers

Why does the implementation of declval in libstdc++-v3 look so complicated?

The code below comes from libstdc++-v3 std::type_traits, which is an implementation of std::declval: template // template 1 _Up __declval(int); template // template 2 _Tp …
expoter
  • 1,622
  • 17
  • 34
18
votes
1 answer

Understanding declval optimized implementation

Looking at libstdc++ source code, I found the following declval implementation: template _Up __declval(int); // (1) template _Tp __declval(long); // (2) template auto declval()…
Igor R.
  • 14,716
  • 2
  • 49
  • 83
15
votes
2 answers

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

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…
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
12
votes
2 answers

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

Consider this example (coming from here): #include #include template struct A { }; struct B { template A f() { return A{}; } using default_return_type =…
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
12
votes
1 answer

std::declval vs crtp, cannot deduce method return type from incomplete type

I am trying to do something like this (in c++11): #include template struct base { using type = decltype( std::declval().foo() ); }; struct bar : base { int foo() { return 42;} }; int main() { bar::type…
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
8
votes
1 answer

Is it legal to use std::declval in lambda in unevaluated contexts?

Code as below or on godbolt compiles with gcc and MSVC but fails with clang. I couldn't find if/where it is forbidden in the standard. In my opinion it should be supported. So who is correct on this, clang or gcc/MSVC? #include void…
wanghan02
  • 1,227
  • 7
  • 14
7
votes
1 answer

std::reference_wrapper, constructor implementation explaination

I have been trying to understand the implementation of std::reference_wrapper, from here, which is as follows: namespace detail { template constexpr T& FUN(T& t) noexcept { return t; } template void FUN(T&&) = delete; } …
warrior_monk
  • 383
  • 2
  • 14
6
votes
3 answers

Is std::declval outdated because of guaranteed copy elision?

The standard library utility declval is defined as: template add_rvalue_reference_t declval() noexcept; To add a rvalue reference here seemed like a good idea, if you think about the language when it was introduced in C++11: Returning a…
Tobi
  • 2,591
  • 15
  • 34
5
votes
1 answer

Using declval with a reference type

I see some code examples where the type used to instantiate the std::declval template function is specified as a reference type rather than just a type, as in: std::declval() as opposed to: std::declval() where T is some type. I am missing…
4
votes
0 answers

How to retrieve the type of a variadic template argument?

I cant find a way to define a type like this: template using hasSomeFunc = decltype(std::declval().SomeFunc(std::declval>(),…
Iñigo
  • 97
  • 6
3
votes
1 answer

For what T does `std::declval()` not have a matching function?

I was surprised to find that for some T, decltype(std::declval()) is not legal: #include template using Alias = decltype(std::declval()); // as expected using A1 = Alias; using A2 = Alias; // error: no…
Eric
  • 95,302
  • 53
  • 242
  • 374
2
votes
0 answers

static assertion on noexcept for protected constructors

I'm attempting to assert following protected class constructors may or may not throw as follows: #include #include class X { protected: X() noexcept(false) { } X(int) noexcept { } }; int main() { // should fail …
metablaster
  • 1,958
  • 12
  • 26
2
votes
2 answers

How should this c++ typedef using decltype and declval be written to make it portable?

I have the following template struct ResultOf { typedef typename decltype(boost::declval()(boost::declval())) Type; }; It was written so that VS2010 could have a result_of that worked for a specific use…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
1
vote
1 answer

error: use of 'decltype(auto) X before deduction of 'auto' (for generated lambda)

I'm trying to hammer down the type of a this-capturing lambda using a special generating member function with decltype(auto). However, the compiler resists to determine the lambda's type for the following contexts that require it, even though it is…
glades
  • 3,778
  • 1
  • 12
  • 34
1
2