decltype是一个C ++ 11关键字,可用于查找表达式的类型。
我正在尝试创建一个模板类,它需要 lambda 作为输入,存储它,并在向量中存储一些 type = lambda 返回类型的元素。 但我不知道如何获得那种类型...
您好,请考虑以下用例: int main() { std::shared_ptrshared_ptr_to_int; std::cout << typeid(int).name() << std::endl; std::cout << typeid(decltype(*
在自定义可调用类模板中使用 decltype(auto) 作为返回类型
我正在自定义可调用类模板中尝试使用 decltype(auto) 来简化 std::function 的行为。我的目标是保持准确的返回类型,包括 cons 这样的限定符...
C++ 中 decltype(dependent-expression) 的唯一依赖类型
我对 decltype(dependent-expression) 确定“唯一依赖类型”的效果有疑问。 其他讨论 stackoverflow_link 涉及这个主题,但它只是
如何提取对象的模板来声明具有不同模板参数的新对象? 如果可能的话,我正在寻找具有以下用法的东西: #包括 如何提取对象的模板来声明具有不同模板参数的新对象? 如果可能的话,我正在寻找具有以下用法的东西: #include <vector> int main() { std::vector<float> a; std::array<int, 7> b; get_template<decltype(a), unsigned> new_a; // std::vector<unsigned> get_template<decltype(b), float, 85> new_b; // std::array<float, 85> return 0; } 如何提取对象的模板来声明具有不同模板参数的新对象? 如果可能的话[...] 是的,您可以执行以下操作: // Primary template template<typename T> struct get_template; // Specialization for std::vector template<typename T, typename Allocator> struct get_template<std::vector<T, Allocator>> { template<typename U, typename UAllocator = std::allocator<U>> using type = std::vector<U, UAllocator>; }; // Specialization for std::array template<typename T, std::size_t N> struct get_template<std::array<T, N>> { template<typename U, std::size_t UN = N> using type = std::array<U, UN>; }; // ... so on for other containers! // Helper alias for both specializations template<typename Container, typename U, auto... Ts> using get_template_t = typename get_template<Container>::template type<U, Ts...>; 你会这样使用它: std::vector<float> a; std::array<int, 7> b; get_template_t<decltype(a), unsigned> new_a; // std::vector<unsigned> get_template_t<decltype(b), float, 85> new_b; // std::array<float, 85> 观看现场演示 您可以专门针对类型和非类型参数的特定组合,但您无法拥有完全通用的解决方案。 // Primary template template<typename T> struct get_template; // Specialization for types with no non-type parameters template<template<typename...> typename Container, typename... Ts> struct get_template<Container<Ts...>> { template<typename... Us> using type = Container<Us...>; }; // Specialization for types with a type parameter followed by non-type parameters template<template<typename, auto...> typename Container, typename T, auto... Ns> struct get_template<Container<T, Ns...>> { template<typename U, auto... UNs> using type = Container<U, UNs...>; }; template<typename Container, typename... Us> using get_template_t = typename get_template<Container>::template type<Us...>; template<typename Container, typename T, auto... Us> using get_template_value_t = typename get_template<Container>::template type<T, Us...>; std::vector<float> a; std::array<int, 7> b; static_assert(std::same_as<get_template_t<decltype(a), unsigned>, std::vector<unsigned>>); static_assert(std::same_as<get_template_value_t<decltype(b), float, 85>, std::array<float, 85>>);
如何提取对象的模板来声明具有不同模板参数的新对象? 如果可能的话,我正在寻找具有以下用法的东西: #包括 如何提取对象的模板来声明具有不同模板参数的新对象? 如果可能的话,我正在寻找具有以下用法的东西: #include <vector> int main() { std::vector<float> a; std::array<int, 7> b; get_template<decltype(a), unsigned> new_a; // std::vector<unsigned> get_template<decltype(b), float, 85> new_b; // std::array<float, 85> return 0; } 您可以按照以下方式进行操作: // Primary template template<typename T> struct get_template; // Specialization for std::vector template<typename T, typename Allocator> struct get_template<std::vector<T, Allocator>> { template<typename U, typename UAllocator = std::allocator<U>> using type = std::vector<U, UAllocator>; }; // Specialization for std::array template<typename T, std::size_t N> struct get_template<std::array<T, N>> { template<typename U, std::size_t UN = N> using type = std::array<U, UN>; }; // ... so on for other containers! // Helper alias for both specializations template<typename Container, typename U, auto... Ts> using get_template_t = typename get_template<Container>::template type<U, Ts...>; 你会这样使用它: std::vector<float> a; std::array<int, 7> b; get_template_t<decltype(a), unsigned> new_a; // std::vector<unsigned> get_template_t<decltype(b), float, 85> new_b; // std::array<float, 85> ``
使用 -std=c++23,gcc 接受以下代码,而 clang 拒绝它: 结构外层{ 结构体{ 整数x; 内联 int get_x() const; } 内部; }; 内联整型 decltype(外部::内部)::get_x()...
查看值类别的定义,我尝试了此处提出的代码片段:https://stackoverflow.com/a/16638081/21691539 模板 结构体值类别{ // 或者可以是
我正在尝试确定各种 C++ 成员函数的返回类型。我知道 decltype 和 std::declval 可用于执行此操作,但我在语法和查找方面遇到问题......
decltype(type) 和 decltype(vector_of_type) 有什么区别?
我遇到了 decltype 的问题,它不适用于向量。 Vector Eq 与holder 和itemGrabbed 的类型相同: 物品* 物品已抓取; 物品*支架; std::vector> 方程;
decltype(type) 和 decltyle(vector_of_type) 有什么区别
我遇到了 decltype 问题,它不适用于向量。 Vector Eq 与holder 和itemGrabbed 的类型相同。 物品* 物品已抓取; 物品*支架; std::vector> 方程;
假设我有一个带有构造函数 T(int b) 的类 T 和带有类似构造函数 Tchild(int b) 的子 Tchild 。 如何将 decltype 与参数一起使用? 例如: 让我们假装我不知道...
如何使用 decltype 从指向数据类型的指针获取数据类型?
decltype 运算符与 type* 一起使用时返回 type&。有没有办法只获取类型。 整数*我; decltype(*i) l; // int& l; 进入; // decltype(???) o; 我只是想知道是否...
我有一个实用函数,用于根据函子转换可交互对象。 模板 自动变换(可迭代 const& 输入,Functor&& trans...
使用 decltype 进行模板化函数声明会导致定义模板化函数时出现“冲突”
我使用decltype来确保模板函数force2与另一个模板函数force1具有相同的参数和返回类型,但它无法编译。我需要将函数模板化。
使用 decltype 进行函数声明会导致定义函数时出现“冲突”
我使用decltype来确保模板函数force2与另一个模板函数force1具有相同的参数和返回类型,但它无法编译。我需要将函数模板化。
这段代码中,a + b 执行了一次还是两次?验证这一点的好方法是什么? double num3 = [](double a, double b)->decltype(a + b) { return a + b; }(1.2,2.1);
在模板函数中,我试图创建一个 std::vector ,其 value_type 依赖于该函数的模板参数的成员函数。该模板参数受到限制...
有没有办法确定成员函数指针的返回类型? 代码示例: ///// 我的图书馆 void my_func(auto mptr) { // 必须使用 `auto` // 一些基于 mptr 返回类型的逻辑:...
C++11:在类中查找方法和错误:嵌套名称说明符中使用的类型不完整
使用 C++11 中的 CRTP 习惯用法,我试图检查派生类是否具有名为 size 的方法。 #包括 模板 结构 IsContainer { 模板<