Questions tagged [declval]

29 questions
1
vote
1 answer

Type trait to check if istream operator>> exists for given type

I found this type trait which can be used to check if a certain type T supports operator<<: template struct has_ostream_operator_impl { template static auto test(V*) -> decltype(std::declval() <<…
ph_0
  • 617
  • 8
  • 27
1
vote
1 answer

How to check copy assignment operator in c++ in noexcept() function

I try to check template class T in function do_math() on the possibility of throwing an exception in the copy assignment operator. But the following code throws an error: template void do_math() noexcept(noexcept(T(std::declval()))…
1
vote
1 answer

What is meant by the statement that std::declval cannot be called?

On the cppreference-page for std::declval it says the following: Return value Cannot be called and thus never returns a value. What does this mean? Surely we call it when we use it? struct Foo { int func(){} …
JensB
  • 839
  • 4
  • 19
1
vote
0 answers

Why isn't std::declval() an lvalue when std::declval() is?

The first of these two lines fails to compile, the second compiles in MSVC 2017: // std::cout << sizeof(decltype(++std::declval())) << "\n"; //error. expression must be a modifiable lvalue. std::cout <<…
TRPh
  • 187
  • 1
  • 9
1
vote
3 answers

Compiler infering the template argument

template class A { public: A(T &t) : t_(t){} T t_; }; int main() { int value; A a(value); // what I wish for : A a(value); // which does not compile "missing template argument before…
Vince
  • 3,979
  • 10
  • 41
  • 69
1
vote
2 answers

Can I use declval to Construct an Unused Return?

Let's say I have a templatized function that is going to be specialized, so I really don't care about the base implementation. Can I do something like this: template T dummy() { assert(false); return declval(); } When I try…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
1
vote
1 answer

Is There a declval for Function Pointers?

I have a function and I need to test whether I can pass an argument of a given type to it. For example: template decltype(F(declval{})) foo(); Calling foo() does 2 things: Sets the return type of foo would have the…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
1
vote
2 answers

Use invoke_result with void argument type?

I'm trying to do the following: struct Unwrapper { template auto operator()(const T& arg, std::enable_if_t, void>* = nullptr) {return arg;} template auto operator()(const T& arg,…
haelix
  • 4,245
  • 4
  • 34
  • 56
1
vote
1 answer

How to use decltype as the LHS of a larger type expression when compiling under VS2010-VS2015

I've got two versions of code both using decltype and declval. One works and one doesn't. They are included below. I've tested this on VS2017 and below and I get the same results. VS2018 will compile it. GCC and Clang both compile it all. The error…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
1
vote
2 answers

Concepts/SFINAE error with typename

I'm trying to make a simply example for myself using new concept syntax. I decided to test if a type had operator() defined, and created a struct to test this using the SFINAE paradigm, but I'm running into type issues. Here is my code: #include…
Anthony
  • 1,015
  • 8
  • 22
1
vote
1 answer

std::declval and unevaluated expressions

With reference to the following example in Proposing Standard Library Support for the C++ Detection Idiom: // primary template handles types that do not support pre-increment template< class, class = void_t<> > struct has_pre_increment_member :…
user9196120
  • 381
  • 3
  • 9
0
votes
1 answer

detection idiom and default parameter type match

I can write the following to detect, if an object is serializable. template struct is_serializable : std::false_type {}; template struct is_serializable () <<…
Martin Fehrs
  • 792
  • 4
  • 13
0
votes
1 answer

Zero Initialize a Type

Given a variable of an unsigned integral type: foo lets say I want to do this: const decltype bar{}; cout << (55834574890LL & ~bar) << endl; That gives me the expected 42. But now let's say that I want to do away with the bar variable. So…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
-1
votes
1 answer

Understanding declval function a little further

I just learn the declval keyword in c++ and I was wondering why in the following code (std::add_rvalue_reference::type).constFunc7() x = 3; is not compiling. Isn't it the same thing as decltype(declvalCustom().constFunc7()) y = 3; with the…
roi_saumon
  • 489
  • 4
  • 13
1
2