decltype

decltype( constexpr variable)

女生的网名这么多〃 提交于 2021-02-16 16:13:27
问题 Why decltype of constexpr variable is failed ? #include <cstdint> #include <type_traits> constexpr uint16_t foo(){ return 0;} constexpr auto cv = foo(); auto v = foo(); static_assert( std::is_same< uint16_t, decltype(cv)>::value, "!"); // failed static_assert( std::is_same< uint16_t, decltype(v) >::value, "!"); // success 回答1: decltype(entity) specifies the declared type of the entity specified by this expression. Due to the constexpr, ( A constexpr specifier used in an object declaration

decltype( constexpr variable)

依然范特西╮ 提交于 2021-02-16 16:13:00
问题 Why decltype of constexpr variable is failed ? #include <cstdint> #include <type_traits> constexpr uint16_t foo(){ return 0;} constexpr auto cv = foo(); auto v = foo(); static_assert( std::is_same< uint16_t, decltype(cv)>::value, "!"); // failed static_assert( std::is_same< uint16_t, decltype(v) >::value, "!"); // success 回答1: decltype(entity) specifies the declared type of the entity specified by this expression. Due to the constexpr, ( A constexpr specifier used in an object declaration

Access a type in a variadic template by index

柔情痞子 提交于 2021-02-10 11:47:38
问题 I would like to obtain a type in a variadic template by index. The index is specified as a template argument. I managed to find a 'hack' that works, but I believe that it is not in the spirit of variadic template programming. Besides, it uses extra memory. Here is the code with some explanations: template <typename... InputPortTypes> class PipelineReceiver { protected: // This tuple is used for storing types only // Hence, I would like to get rid of it, but I am not sure how. std::tuple< std:

Access a type in a variadic template by index

巧了我就是萌 提交于 2021-02-10 11:45:09
问题 I would like to obtain a type in a variadic template by index. The index is specified as a template argument. I managed to find a 'hack' that works, but I believe that it is not in the spirit of variadic template programming. Besides, it uses extra memory. Here is the code with some explanations: template <typename... InputPortTypes> class PipelineReceiver { protected: // This tuple is used for storing types only // Hence, I would like to get rid of it, but I am not sure how. std::tuple< std:

decltype(some_vector)::size_type doesn't work as template parameter

末鹿安然 提交于 2021-01-27 07:46:37
问题 The following class does not compile: template<class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key>> class MyContainer { public: std::vector<Key, Allocator> data; std::vector<std::pair<std::size_t, decltype(data)::size_type>> order; }; I get the following compiler error: error: type/value mismatch at argument 2 in template parameter list for ‘template struct std::pair’ Why does that fail to compile, while the following code works fine? template<class Key, class

Get decltype of function

末鹿安然 提交于 2020-07-28 13:03:27
问题 I want to get the type of a function and create a std::vector of it. For example, I have int foo(int a[], int n) { return 1; } int bar(int a[], int n) { return 2; } and a vector of functions like this would be: std::vector< std::function<int(int[],int)> > v; And in general, a decltype() would be better, like: std::vector< decltype(foo) > v; however, this will result in a compilation error. I guess the reason is that decltype() cannot distinguish between int (*func)(int[], int) std::function

Is it allowed to use decltype in an initializer for the variable that is decltyped?

大憨熊 提交于 2020-04-11 06:11:10
问题 Triggered by this question, I was wondering if, this is allowed: template <typename T> T foo(){return T{};} struct bar {}; int main() { bar a = foo<decltype(a)>(); } Compilers I tried took it without complaints, but I am not sure if this is really legal or if I am missing any pitfalls (and it looks weird to use the type of a during its declaration). For background: in the linked question OP wants to avoid auto and spelling out the type (here it is bar , SomeComplexTypeAndNotAuto in that

Is it allowed to use decltype in an initializer for the variable that is decltyped?

点点圈 提交于 2020-04-11 06:10:52
问题 Triggered by this question, I was wondering if, this is allowed: template <typename T> T foo(){return T{};} struct bar {}; int main() { bar a = foo<decltype(a)>(); } Compilers I tried took it without complaints, but I am not sure if this is really legal or if I am missing any pitfalls (and it looks weird to use the type of a during its declaration). For background: in the linked question OP wants to avoid auto and spelling out the type (here it is bar , SomeComplexTypeAndNotAuto in that

How can I determine the return type of a C++11 member function

时光毁灭记忆、已成空白 提交于 2020-02-21 10:12:26
问题 I am trying to determine the return type of a various C++ member functions. I understand that decltype and std::declval can be used to do this, but I am having problems with the syntax and finding useful examples. The TestCBClass below shows an example of a dumb class that contains a mixture static and normal member functions - with & without arguments and return types. Depending on the method in question, I would like to be able to declare a vector of return types from each of the various

How can I determine the return type of a C++11 member function

╄→尐↘猪︶ㄣ 提交于 2020-02-21 10:09:59
问题 I am trying to determine the return type of a various C++ member functions. I understand that decltype and std::declval can be used to do this, but I am having problems with the syntax and finding useful examples. The TestCBClass below shows an example of a dumb class that contains a mixture static and normal member functions - with & without arguments and return types. Depending on the method in question, I would like to be able to declare a vector of return types from each of the various