auto 相关问题

`auto`关键字在C ++ 11中用于推导类型。当用于替换初始化变量声明中的类型名称时,该变量的类型与初始化程序相同。当用作返回类型时,返回类型被指定为尾随返回类型,或者从return-expression推导出。

C++ 中强化的“auto”

有一次我意识到我希望能够编写这样的代码而不重复代码: 模板 std::ostream& 运算符<< (std::ostream& o, const

回答 1 投票 0

如何将数据从一个工作表提取到另一列包含特定日期的工作表?

我正在努力使用 Excel 电子表格,希望有人可以提供帮助。 它有两个工作表,“数据库”和“打印表” 数据库中有几列,A:A cont...

回答 1 投票 0

为什么我可以在 c++11 中使用推导的返回类型而不使用尾随返回类型?

我了解到自动推导返回类型是c++14的一个特性,就像下面一样 模板 自动发布(Func func, Args...args); 有人告诉我,如果我愿意我们...

回答 1 投票 0

为什么“自动”和显式引用变量声明的行为不同?

我有这样的事情: 酒吧类; 类 Foo() { 民众: Foo() : 酒吧(新酒吧()); Bar& GetBar() { return *bar.get(); } } 私人的: std::unique_ptr 栏; }; 无效主() {...

回答 3 投票 0

为什么自动变量在用字符串初始化时会推导为指针?

#包括 #包括 int main() { const char a[] = "你好世界"; const char * p = "你好世界"; 自动x =“你好世界”; ...

回答 3 投票 0

将多个 EC2 实例从 IMDSv1 转换到 IMDSv2

有没有一种方法可以更新一个帐户上的 100 个 EC2 实例,而不必在每个实例上使用带有 --instance-id 的修改实例元数据选项? 我环顾四周,心想肯定是……

回答 1 投票 0

我可以在cpp中使用模板或auto关键字来创建一个变量,我可以将其与std::cin一起使用以从用户那里获取值吗?

这行代码是错误的,无法编译,但我想知道是否可以修复它。 #包括 模板 自动获取值() { std::cout << "Enter an

回答 1 投票 0

用户退出后如何自动登录?

我正在为客户构建一个信息屏幕。有时,用户可能会被注销,例如更新我们的系统时。如何保证用户立即自动重新登录? ...

回答 1 投票 0

为什么将函数指针指向 `operator new` 在 C++14 中可以编译,但在 C++17 中却不能编译?

考虑以下代码(godbolt 链接): #包括 int main() { 自动 foo = 运算符 new; 无效* mem = foo(1); printf("%p", 内存); } 该代码在 GCC 上编译,...

回答 1 投票 0

google-services.json 用于 Google-play-services:ads - 我应该生成吗?

我在旧应用程序中使用 Google Play 服务(广告),更新库后遇到问题 自动点击(停留时间) 什么地方出了错: 任务 ':app:processLiteReleaseGoogleService... 执行失败...

回答 1 投票 0

Range-for-loops 和 std::vector<bool>

为什么这段代码有效 std::vector intVector(10); for(自动& i : intVector) std::cout << i; And this doesn't? std::vector boolVector(10); 对于(自动&我:

回答 3 投票 0

C++11 - 将非静态数据成员声明为“auto”

如果非静态数据成员在声明中被初始化,C++11 是否允许将它们声明为“auto”?例如: 结构S { 汽车 x = 5; // 代替 'int x = 5;',这绝对是...

回答 3 投票 0

为什么 std::integral auto&& 参数不适用于左值参数

我不确定为什么下面的代码不能编译: #包括 #包括 #包括 #包括 #包括 #包括 我不确定为什么下面的代码不能编译: #include <array> #include <algorithm> #include <type_traits> #include <concepts> #include <cstdio> #include <cwchar> template <typename T> concept Character = std::same_as< std::remove_cv_t<T>, char > || std::same_as< std::remove_cv_t<T>, signed char > || std::same_as< std::remove_cv_t<T>, unsigned char > || std::same_as< std::remove_cv_t<T>, wchar_t > || std::same_as< std::remove_cv_t<T>, char8_t > || std::same_as< std::remove_cv_t<T>, char16_t > || std::same_as< std::remove_cv_t<T>, char32_t >; template <std::size_t N> consteval auto create_character_array( std::integral auto&& fill_character ) noexcept { std::array<std::remove_cvref_t<decltype( fill_character )>, N> dashes { }; std::fill( std::begin( dashes ), std::end( dashes ), fill_character ); return dashes; } int main( ) { const char ch { '^' }; auto dashes { create_character_array<128>( ch ) }; dashes.back( ) = '\0'; std::fputs( std::data( dashes ), stdout ); } 我不确定,但由于某种原因,通用参考(auto&&)和概念std::integral似乎效果不佳。用我自己更精确的概念 std::integral 替换 Character 也无济于事。 出现以下错误: <source>: In function 'int main()': <source>:30:46: error: no matching function for call to 'create_character_array<128>(const char&)' 30 | auto dashes { create_character_array<128>( ch ) }; | ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~ <source>:19:16: note: candidate: 'template<long unsigned int N, class auto:16> requires integral<auto:16> consteval auto create_character_array(auto:16&&)' 19 | consteval auto create_character_array( std::integral auto&& fill_character ) noexcept | ^~~~~~~~~~~~~~~~~~~~~~ <source>:19:16: note: template argument deduction/substitution failed: <source>:19:16: note: constraints not satisfied In file included from /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/compare:37, from /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/array:38, from <source>:1: /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/concepts: In substitution of 'template<long unsigned int N, class auto:16> requires integral<auto:16> consteval auto create_character_array(auto:16&&) [with long unsigned int N = 128; auto:16 = const char&]': <source>:30:46: required from here /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/concepts:100:13: required for the satisfaction of 'integral<auto:16>' [with auto:16 = const char&] /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/concepts:100:24: note: the expression 'is_integral_v<_Tp> [with _Tp = const char&]' evaluated to 'false' 100 | concept integral = is_integral_v<_Tp>; | ^~~~~~~~~~~~~~~~~~ ASM generation compiler returned: 1 <source>: In function 'int main()': <source>:30:46: error: no matching function for call to 'create_character_array<128>(const char&)' 30 | auto dashes { create_character_array<128>( ch ) }; | ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~ <source>:19:16: note: candidate: 'template<long unsigned int N, class auto:16> requires integral<auto:16> consteval auto create_character_array(auto:16&&)' 19 | consteval auto create_character_array( std::integral auto&& fill_character ) noexcept | ^~~~~~~~~~~~~~~~~~~~~~ <source>:19:16: note: template argument deduction/substitution failed: <source>:19:16: note: constraints not satisfied In file included from /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/compare:37, from /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/array:38, from <source>:1: /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/concepts: In substitution of 'template<long unsigned int N, class auto:16> requires integral<auto:16> consteval auto create_character_array(auto:16&&) [with long unsigned int N = 128; auto:16 = const char&]': <source>:30:46: required from here /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/concepts:100:13: required for the satisfaction of 'integral<auto:16>' [with auto:16 = const char&] /opt/compiler-explorer/gcc-trunk-20230509/include/c++/14.0.0/concepts:100:24: note: the expression 'is_integral_v<_Tp> [with _Tp = const char&]' evaluated to 'false' 100 | concept integral = is_integral_v<_Tp>; | ^~~~~~~~~~~~~~~~~~ 当'^'作为参数传递时编译。然而ch使它失败。这是在将 std::integral 添加到函数定义之后发生的。没有它,代码编译。 万一这不是一个好的设计,我应该如何定义上述功能以使其尽可能通用?我希望它接受任何值类别。

回答 0 投票 0

Yii 2 注销 Yii 2 注销

我正在寻找一种在 30 秒或 1 分钟不活动后注销用户的方法。 我是 Yii 2 的新手。请帮助我。 提前致谢。

回答 0 投票 0

指定返回类型时为什么不能自动推断类型?

MRE: std::vector someFunction() { 自动向量; 返回向量; } 是什么阻止“自动”将 vec 的类型推断为 std::vector?

回答 1 投票 0

错误:无法安装软件包,它说无法验证证书我该怎么办?

错误:由于 OSError 无法安装软件包: HTTPSConnectionPool(host='github.com', port=443):超出最大重试次数 带网址: /explosion/spacy-models/releases/download/en_core_web_sm-3.4.0/

回答 0 投票 0

为什么 auto on grid-template-columns 使多个元素具有相同的宽度?

我想做什么 项目列表(不是 ul/ol 类型的列表)位于页面的中央,这些项目有 2 列。第一列应该有一个“固定”宽度(1em 在这...

回答 1 投票 0

使用 auto 和 decltype 从类模板中的成员函数返回引用

如何使用 auto/decltype 强制模板类中的函数返回对成员变量的引用? 这是我正在尝试做的一个简单的例子。假设你有一个模板化的

回答 2 投票 0

M1 Mac - 使用 CodeRunner 的 VSCode - 不能使用自动迭代器计算向量元素[重复]

尝试运行一个返回两个数组并集的 cpp 程序。计算机是 M1 Pro Macbook Pro 14.2 英寸,Mac OS Monterey 12.6。将 VSCode 与 C/C++ 扩展和 Coderunner 结合使用。我...

回答 0 投票 0

使用基于范围的for循环放置元素时获取随机数

我用 CLion 编写了一个程序来在 C++ 中插入数字。 首先,输入一组正整数并将它们放入名为 pour 的向量中。 然后,取这些正整数的负数...

回答 0 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.