Questions tagged «c++»

C ++是一种通用编程语言。它最初被设计为C的扩展,并且具有类似的语法,但是现在它是一种完全不同的语言。使用此标记可解决有关将要使用C ++编译器编译的代码的问题。对于与特定标准修订版[C ++ 11],[C ++ 14],[C ++ 17]或[C ++ 20]等相关的问题,请使用特定于版本的标记。

7
删除向量中的元素不起作用
我有一个向量。我需要删除其中的最后3个元素。描述了这种逻辑。程序崩溃。可能是什么错误? vector<float>::iterator d = X.end(); for (size_t i = 1; i < 3; i++) { if (i == 1) X.erase(d); else X.erase(d - i); }
10 c++  vector 

1
通过C ++中的变量访问结构中的成员
我有两个成员的结构,例如: struct DataSet { int x; int y; }; ...,而我必须以一种方法来访问它们,但一次只能访问一个,例如: void foo(StructMember dsm) { // ("StructMember" does not exist) DataSet ds; ds.x = 4; ds.y = 6; std::cout << ds.dsm * ds.dsm << std::endl; } foo(x); foo(y); 我希望输出: 16 36 当我必须解决此类问题时该怎么办?是否存在可以访问成员的数据类型?
10 c++ 

2
C ++模板模板参数类型推导
我有在字符串容器中查找并打印出模式匹配项的代码。在模板化的函数foo中执行打印 编码 #include <iostream> #include <algorithm> #include <iterator> #include <vector> #include <string> #include <tuple> #include <utility> template<typename Iterator, template<typename> class Container> void foo(Iterator first, Container<std::pair<Iterator, Iterator>> const &findings) { for (auto const &finding : findings) { std::cout << "pos = " << std::distance(first, finding.first) << " "; std::copy(finding.first, finding.second, std::ostream_iterator<char>(std::cout)); …

1
为什么std :: span缺少比较运算符?
std::span设计不是作为对std::vector/ std::array/ 普通数组等子区域的轻量级引用吗?它是否不应该在其API中也包含比较运算符,以便与它们保持一致?排除在外的原因是什么? 注:通过比较操作,我的意思是无论是全套(<,<=,...),或者太空飞船<=>
10 c++  std  c++20  std-span 

1
自C ++ 20起允许在分配的存储上使用指针算术吗?
在C ++ 20标准中,据说数组类型是隐式生存期类型。 这是否意味着可以隐式创建非隐式生命周期类型的数组?这样的数组的隐式创建会不会导致创建数组的元素? 考虑这种情况: //implicit creation of an array of std::string //but not the std::string elements: void * ptr = operator new(sizeof (std::string) * 10); //use launder to get a "pointer to object" (which object?) std::string * sptr = std::launder(static_cast<std::string*>(ptr)); //pointer arithmetic on not created array elements well defined? …

3
不同编译器中C ++和C之间无符号位域整数表达式的截断不一致
编辑2: 当以前驻留在C ++源文件中但完全移入C文件的函数开始返回不正确的结果时,我正在调试一个奇怪的测试失败。下面的MVE允许重现GCC问题。但是,当我一时兴起,用Clang(后来又用VS)编译示例时,得到了不同的结果!我无法弄清楚是将其视为编译器之一中的错误,还是C或C ++标准允许的未定义结果的体现。奇怪的是,没有一个编译器给我有关该表达式的任何警告。 罪魁祸首是这样的表达: ctl.b.p52 << 12; 在这里,p52键入为uint64_t;它也是工会的一部分(见control_t下文)。移位操作不会丢失任何数据,因为结果仍然适合64位。但是,如果我使用C编译器,那么GCC决定将结果截断为52位!使用C ++编译器,将保留所有64位结果。 为了说明这一点,下面的示例程序用相同的主体编译了两个函数,然后比较了它们的结果。c_behavior()放在C源文件和cpp_behavior()C ++文件中,并main()进行比较。 带有示例代码的存储库:https : //github.com/grigory-rechistov/c-cpp-bitfields 标头common.h定义64位宽位域和整数的并集,并声明两个函数: #ifndef COMMON_H #define COMMON_H #include <stdint.h> typedef union control { uint64_t q; struct { uint64_t a: 1; uint64_t b: 1; uint64_t c: 1; uint64_t d: 1; uint64_t e: 1; uint64_t f: 1; uint64_t g: …

1
为什么范围不能用于管道库功能?
乔纳森博卡拉(作者流利C ++)写了一个叫做库的管道。 存储库主页上说的“管道”与范围的使用不同,尽管看起来相同:它不是基于惰性拉动,而是急于推动。但有人指出,不能使用范围库执行各种“管道”操作。例如: 解压缩-采取一个压缩输入-本质上是一系列k元组-并产生k个独立的独立输出。 fork-产生容器/范围的多个(独立)副本。 我不太明白原则上为什么会这样。(当然,除了无法获得最终迭代器/前哨的范围之外。)

3
可以将nullptr转换为uintptr_t吗?不同的编译器不同意
考虑以下程序: #include <cstdint> using my_time_t = uintptr_t; int main() { const my_time_t t = my_time_t(nullptr); } 无法使用msvc v19.24进行编译: <source>(5): error C2440: '<function-style-cast>': cannot convert from 'nullptr' to 'my_time_t' <source>(5): note: A native nullptr can only be converted to bool or, using reinterpret_cast, to an integral type <source>(5): error C2789: 't': …
10 c++  c++11  gcc  visual-c++  clang 

1
概念TS检查将忽略私有访问修饰符
我想写一个可索引的概念,意味着一个序列要么具有返回RandomAccessIterator的开始/结束,要么定义了operator []并返回非空类型的值。 我将Stroustrup的文章中的想法用于序列概念,并将其扩充为: template <class T> concept bool Indexable = Sequence<T> || requires(T t, size_t n) { { t[n] } -> NotVoid; }; 它适用于大多数情况,但不适用于以下情况: struct Bad { std::vector<int> nums; private: int& operator[](size_t ind) { return nums[ind]; } }; static_assert(!Indexable<Bad>, "fail"); 由于某种原因,我的概念忽略了operator []被定义为私有并返回true的事实。我想念什么?
10 c++  c++-concepts 

1
GCC / C ++ 17中带有模板模板类的问题
考虑以下2个重载 template<typename T> bool test() { return true; } template<template<typename ...> class T> bool test() { return false; } 第一个适用于常规类,而第二个适用于未实例化的模板。例如: std::cout<<test<int>()<<std::endl; <-- this yields 1 std::cout<<test<std::list>()<<std::endl; <--this yields 0 现在考虑以下模板函数: template<typename U> bool templfun(){ struct A{ bool f(){ return test<A>(); // <-- this gives an error } }; return test<A>(); …
10 c++  templates  gcc  clang  c++17 

4
为什么要从main()返回NULL?
我有时会看到在C和C ++程序中NULL用作返回值的编码器,main()例如: #include <stdio.h> int main() { printf("HelloWorld!"); return NULL; } 当我用gcc编译此代码时,得到以下警告: 警告:return从指针转换为整数而不进行强制转换[-Wint-conversion] 这是合理的,因为宏NULL应扩展为,(void*) 0并且main的返回值应为类型int。 当我制作一个简短的C ++程序时: #include <iostream> using namespace std; int main() { cout << "HelloWorld!"; return NULL; } 并使用g ++进行编译,我得到了等效的警告: 警告:从NULL转换为非指针类型'int'[-Wconversion-null] 但是,为什么当警告NULL发生main()时将它们用作返回值呢?这只是不好的编码风格吗? 尽管有警告,仍要使用NULL而不是将其0用作返回值的原因是什么main()? 它是否是实现定义的,是否合适?如果是,为什么任何实现都希望返回指针值?
10 c++  c  null  return  return-value 


1
我们将能够使用C ++ 20构建具有视图的容器吗?
C ++ 20标准版本将范围引入C ++。 我的问题:我们能否构造(现有)任何范围的标准库容器?更重要的是,具有范围视图吗? 例如,这将: #include <vector> #include <iostream> #include <ranges> int main() { auto sq = [](int x) { return x * x; }; std::vector<int> vec { 3, 4, 5 }; std::vector<int> squares { std::ranges::views::transform(vec, sq) }; for(auto i : squares) { std::cout << i << ' '; …
10 c++  c++20  range-v3 

1
在使用GCC的unordered_map :: insert KeyEqual异常期间发生内存泄漏-破坏了强有力的异常安全保证吗?
我正在使用GCC 7.3.1,但也在coliru(我相信它是9.2.0版)上进行了测试。使用以下内容进行构建: g++ -fsanitize=address -fno-omit-frame-pointer rai.cpp 这里是rai.cpp: #include <iostream> #include <unordered_map> int main() { try { struct MyComp { bool operator()(const std::string&, const std::string&) const { throw std::runtime_error("Nonono"); } }; std::unordered_map<std::string, std::string, std::hash<std::string>, MyComp> mymap; mymap.insert(std::make_pair("Hello", "There")); mymap.insert(std::make_pair("Hello", "There")); // Hash match forces compare } catch (const std::exception& e) { …


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.