Questions tagged [decltype]
decltype is a C++11 keyword that can be used to find the type of an expression.
734
questions
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
...
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:...
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>()
...
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 ...
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 ...
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?
...
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), ...
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::...
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=...
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* ...
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....
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 ...
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::...
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{};...
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 : ...
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
{
...
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 ...
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 ...
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 ...
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);
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{
};
...
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 ...
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::...
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__ <&...
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&...
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 ...
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?
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 ...
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 ...
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 ...
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 ...
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 ...
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(...
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() {...
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)> ...
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::...
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 ...
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 ...
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>
#...
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 ...
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 ...
-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 ...
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(...
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 ...
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
...
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 ...
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 ...
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 { ...
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 ...
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 ...