Questions tagged [decltype]

decltype is a C++11 keyword that can be used to find the type of an expression.

Filter by
Sorted by
Tagged with
1 vote
1 answer
52 views

Why the combination of decltype and is_same gives wrong result?

I have the following function: template<typename T> auto print_type_info(const T& t) { // C++17 if constexpr (std::is_same<T, int>::value) { return t + 1; // int case ...
CPW's user avatar
  • 181
1 vote
2 answers
96 views

Can you do type inference based on a function, without providing parameters?

It's sometimes very useful to create a variable of the correct type where you don't want to explicitly specify the type, particulary when the type is a complex one such as std::map<std::string, std:...
paxdiablo's user avatar
  • 860k
3 votes
1 answer
102 views

How does `decltype(true ? std::declval<T1>() : std::declval<T2>())` works?

I saw the example code for template function max. template <typename T1, typename T2, typename RT = std::decay_t<decltype(true ? std::declval<T1>() ...
myoldgrandpa's user avatar
0 votes
1 answer
120 views

How to define an internal alias of the class using the return type of its own method?

Consider the code below: struct LambdaWrapper { auto getLambda() { return []() { std::cout << "Lambda is called" << std::endl; }; } }; void ...
Dmitry Kuzminov's user avatar
3 votes
4 answers
119 views

How to use decltype(T::member) for a member that might not exist in T

I am looking for a way to define a variable with a type depending on the type of the member of a type my class is templated on, with the additional caveat that the member variable might not exist. As ...
DottyPhone's user avatar
11 votes
2 answers
326 views

Is a leading return type method declaration, using decltype, compatible with a trailing return type definition?

When using decltype, is it permissible to use the traditional leading return type syntax in a declaration: decltype(expr) foo(); and then use C++11 trailing return type syntax in the definition? ...
Scott McPeak's user avatar
  • 9,458
3 votes
2 answers
113 views

Regarding decltype() for captured entities, which compiler is ISO compliant?

#include <iostream> #include <type_traits> int main(){ int i = 1; int& j = i; auto f2 = [j = j]() { std::cout << std::is_same_v<decltype(j), ...
余国良's user avatar
  • 125
1 vote
1 answer
51 views

is google::protobuf::RepeatedField<int>::iterator incrementable? compare to const int*/int*

What's the difference in type deduction? The second line fails, but why? decltype(++std::declval<int*&>()) pri = {nullptr}; decltype(++(std::begin(std::declval<google::protobuf::...
Kamil Płani's user avatar
0 votes
1 answer
105 views

What is the meaning of function`->decltype()` [duplicate]

I've seen this one function I have no idea what's going on here: template <typename Container> auto MaxElement(Container &c,int num_of_el)->decltype(c[0]){ int index=0; for(int i=...
bb_823's user avatar
  • 143
5 votes
1 answer
131 views

Decltype on static variable in template class

I'm trying to define a static variable of a templated class outside of the class scope using clang: class Bar { public: float a; }; template<long count> class Foo { public: static Bar* ...
xEric_xD's user avatar
  • 516
4 votes
1 answer
138 views

decltype, dyn, impl traits, and how to declare the return type of a function when refactoring

I'm a Rust newbie, but programming veteran approaching Rust from a C++ point of view/background, so that may explain much of my confusion. I have at its heart, a mind-numbingly simple problem to solve....
lewis's user avatar
  • 1,156
5 votes
2 answers
286 views

Does using concepts allow using decltype on member function

I came to know that for a class X with a member function named func, the c++ standard doesn't allow us to write decltype(X::func). Thus I expected the below given program to produce an error saying ...
Val's user avatar
  • 148
0 votes
0 answers
85 views

Why does decltype fail to declare a reference type in lambda arguments?

I encountered an issue where I need to use std::visit() on two std::variant objects which are guaranteed to host the same type at the time of the call. The following code compiles without issues: std::...
Phiv's user avatar
  • 1
0 votes
1 answer
77 views

Decltype of function call discards const qualifiers

Consider a function template <typename T> const T&& foo(); When i had tried to tested it,i founded result that I didn't understand. Output is 0. #include <iostream> class C{};...
zxc123's user avatar
  • 3
0 votes
0 answers
51 views

template struct declared twice for checking method existence

I am trying to understand that block of code found in an answer of this question : template template class, call a function if it exists template <typename T, typename = int> struct HasReserve : ...
Arnaud Mégret's user avatar
2 votes
1 answer
94 views

decltype evaluating the wrong type from an expression list

while experimenting with the answer from This post, ended up with the following piece of code: #include <iostream> #include <typeinfo> namespace Test { struct MyPtr { ...
J.M's user avatar
  • 309
0 votes
0 answers
87 views

gcc compiler bug using std::forward<decltype(args)>(args)... for parameter pack in lambda

In the following snippet I try to create an API for the async function which can be provided by a functor and an arbitrary amount of parameters that will be used to invoke a function call in ...
glades's user avatar
  • 3,935
0 votes
1 answer
166 views

Using decltype for lambda comparators

I'm trying to understand the difference in the following two cases. In both, I introduce a comparator as a lambda function: auto comp = [](int x, int y) { return x%2 < y%2; }; But then, if I want ...
LazyCat's user avatar
  • 496
2 votes
1 answer
66 views

Why cant i use alias type for constructing? C++

I have a Class called SceneManager with a method update that when the exit flag is true it switches the current scene to the using next = nextScene, but i don't know why it doesn't work, and the ...
Alvaro Gallo's user avatar
0 votes
1 answer
133 views

Does `decltype` also perform evaluation of the expression?

In this code, is a + b executed once or twice? What would be a good method to verify that? double num3 = [](double a, double b)->decltype(a + b) { return a + b; }(1.2, 2.1);
henry3695's user avatar
0 votes
1 answer
72 views

Getting type of Class inside a Class Method

I want to determine the type of this variable inside a class method. For example, take the following example - #include <iostream> #include <cstdlib> #include <map> class Base{ }; ...
Atharva Dubey's user avatar
0 votes
1 answer
39 views

Decltype error - doesn't see method declaration

I'm trying to use decltype, but I got the same error every time when I was trying compile. #include <iostream> #include <cmath> using namespace std; class Polygon { private: double ...
lelumpolelum's user avatar
1 vote
3 answers
111 views

Accessing variable template using decltype

A minimized example of my code showing the problem: #include <cassert> #include <iostream> #include <map> #include <string> template <typename T> const std::map<std::...
Zitrax's user avatar
  • 19.3k
1 vote
1 answer
66 views

C++ decltype failed on vector elem [closed]

template <typename T> void F(T&) { std::cout << __FUNCTION__ << "\n"; } template <typename T> void F(vector<T>&) { std::cout << __FUNCTION__ <&...
haifeng yang's user avatar
0 votes
0 answers
59 views

Declare different typename once based on string

Lets say I have a function that is defined as Edit: Added more details and reframed the question, thanks for the input template <typename A, typename B> void doSomething(string str, vector<A&...
Sahl 's user avatar
  • 27
0 votes
1 answer
16 views

Infer type information inside if-constexpr

I want to explore the feature of if-constexpr and try to figure out type information at compile-time. For this purpose, I write the following code. I expect that printTypeInfo function will return 4 ...
Wang's user avatar
  • 13
0 votes
1 answer
44 views

Is there an advantage of using parentheses in decltype over explicitly typing const ref

Which is better: decltype((foo)) x = bar; const decltype(foo)& x = bar; If these are just two ways of saying the same thing, why are the parentheses even part of the language?
TwistedBlizzard's user avatar
6 votes
1 answer
92 views

decltype does not preserve ref qualifier from structured binding

Usually decltype perseveres the ref qualifiers auto a = 0; auto& a_ref = a; static_assert(std::is_reference_v<decltype(a_ref)>); But apparently not when it's argument is obtained from ...
Tom Huntington's user avatar
0 votes
0 answers
120 views

Detect if a C compiler supports __typeof and/or decltype

The uthash library (which I maintain) currently uses this snippet to define a macro DECLTYPE(x) which expands to a C-style cast to x's type, or else to define NO_DECLTYPE: /* These macros use decltype ...
Quuxplusone's user avatar
  • 24.4k
2 votes
1 answer
262 views

Why doesn't decltype(*this)::value_type compile? [duplicate]

Why doesn't decltype(*this) compile? It shows an error message: error: 'value_type' is not a member of 'const Foo<char>&' So what exactly is the reason that decltype( *this )::value_type ...
digito_evo's user avatar
  • 3,276
8 votes
2 answers
215 views

Why isn't the original value getting incremented twice even though I have two increments

I'm new at programming and can someone explain to me how this code work? #include <iostream> using namespace std; int main () { int a = 3, b = 4; decltype(a) c = a; decltype((b)) d ...
Maki's user avatar
  • 101
0 votes
1 answer
56 views

Program crashes when using custom compartor for std::set

I tried to use custom comparator in a std::set. When I insert cuisine "japaneses" in a variable bucketCuisines, I get error DEADLYSIGNAL. But, If i eliminate the custom comparator cmp there ...
Eunno An's user avatar
15 votes
2 answers
655 views

Why use decltype on a template argument?

In https://github.com/stlab/libraries/blob/main/stlab/concurrency/main_executor.hpp, I read struct main_executor_type { using result_type = void; template <typename F> void operator(...
le migou's user avatar
  • 471
3 votes
2 answers
710 views

Getting the decltype of a member function?

So the following code works great: void function() { } std::cout << typeid(decltype(function)).name() << '\n'; but the following code doesn't compile: struct object { void function() {...
Nik Tedig's user avatar
  • 453
2 votes
0 answers
79 views

Strip __attribute__((pcs("calling_convention"))) for use with std::function

I have the the following (stripped) code: #include <functional> namespace A{ void __attribute__((pcs("aapcs-vfp"))) foo(); }; int main(){ std::function<decltype(A::foo)> ...
user7005976's user avatar
0 votes
0 answers
189 views

Emulating template constraints in C++14

I'm coding in C++14 a program that uses template classes and functions. I don't like the compiler error messages I get when the template types don't fulfill the requirements I ask for with std::...
Rodrigo Torres's user avatar
1 vote
1 answer
78 views

Strip modifiers from decltype(*this) for use in trailing return type

Let's say I have Base and Derived classes: class Base { public: virtual auto DoSomething(const Base&) const -> decltype(*this) = 0; }; class Derived : public Base { public: const ...
Sean's user avatar
  • 380
1 vote
0 answers
85 views

Is there a way to create a scope in a template or decltype in c++17, for a unit librairy?

Does anyone know if there is any feature in c++17 that would allow you to create a scope (namespace, class or function) inside of a template or decltype() declaration? (note: lambdas don't work inside ...
azyo's user avatar
  • 21
1 vote
1 answer
498 views

C++ template result_of_t no type named 'type' when trying to find return value of function

I'm trying to get the return type of a function template. One solution I saw uses result_of_t: #include <iostream> #include <vector> #include <string> #include <type_traits> #...
Huy Le's user avatar
  • 1,559
2 votes
1 answer
364 views

C++ template type_trait enable_if a class is a map

#include <iostream> #include <any> #include <vector> #include <map> #include <unordered_map> #include <string> using namespace std; // enable_if_t = MapType ...
Huy Le's user avatar
  • 1,559
0 votes
1 answer
326 views

How to deduce a return type in C++

I want to create some kind of Variant in C++. Actually I want to use templates as less as possible. The idea is to store the value in union both with the type of the variable and return the value ...
folibis's user avatar
  • 12.3k
-1 votes
2 answers
60 views

auto/ decltype Error I'm curious why the code doesn't work. (E0252)

int main() { double x[3] = { 1,2,3 }; auto n1 = x[0]; decltype(n1) d1 = n1; decltype(n1) d2; // ok decltype(x[0]) d3; // error } I am a beginner user who uses stack overflow ...
아싸아's user avatar
0 votes
1 answer
368 views

Error: 'decltype' cannot resolve address of overloaded function

I'm trying to glean the return type of a class method inside a class that could either be const or non-const. This information is transfered to another calling class object where I try to use decltype(...
glades's user avatar
  • 3,935
1 vote
0 answers
133 views

How to access the type of the outer class for a type?

I have legacy code that I cannot change: namespace ns1 { struct ObjectType { enum class Value { VALUE1, VALUE2 }; static const char* toString(Value); }; struct Object { ObjectType::Value ...
Dmitry Kuzminov's user avatar
1 vote
3 answers
442 views

auto return types in C++ function vs specifying return type

In my work's codebase, I see the following class custom { auto set_data_type(custom_type_t type_t) -> custom & { // set some stuff // return *this; } } Why can't we simply just do ...
user5965026's user avatar
0 votes
2 answers
184 views

C++ set with customized comparator crashes on insert

STL set can have customized comparator. It can be implemented in several ways, such as define an operator(), use decltype on lambda, etc. I was trying to use a static method of a class and encountered ...
Hongzheng Wang's user avatar
0 votes
2 answers
275 views

Initialize and deduce type of shared_ptr returned by lambda in template class C++14/17

I initially had a working code. Within this class I had code something like this: auto event = std::dynamic_pointer_cast<EventA>(event); Now, to avoid a lot of duplicate code, I extended ...
Abhi Sharma's user avatar
8 votes
1 answer
781 views

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 <class T> constexpr T& FUN(T& t) noexcept { ...
warrior_monk's user avatar
1 vote
2 answers
221 views

why std::decltype is returning reference to a named lvalue object?

I read in Scott Meyer's effective C++ that for lvalue expressions of type T other than names, decltype always reports a type of T&, which i seem to understand (explained here too). However, I am ...
warrior_monk's user avatar
2 votes
1 answer
307 views

Way for class template to deduce type when constructing an instance with std::make_unique?

Let's say we have a class template Foo, that has one type template parameter that it can deduce from an argument in its constructor. If we use std::make_unique to construct an instance of Foo, is ...
JensB's user avatar
  • 911

1
2 3 4 5
15