草庐IT

-std=gnu99

全部标签

带有 std::is_base_of 的派生类的 C++ 模板函数

我在为给定类型创建函数时遇到问题,如果它是从其他类型派生的,那么它会做某事,而对于所有其他情况,则做其他事情。我的代码:classBaseClass{};classDerivedClass:publicBaseClass{};templatevoidFunction(typenamestd::enable_if::value,T>::type&&arg){std::coutvoidFunction(T&&arg){std::cout对于DeriviedClass类和其他基于BaseClass的类,我想调用函数coutingProper,但是它couts不正确。有什么建议吗?

c++ - 编译器不推导出模板参数(map std::vector -> std::vector)

我有以下模板。templatestd::vectormap(conststd::vector&v,std::functionf){std::vectorres;res.reserve(v.size());std::transform(std::begin(v),std::end(v),std::end(res),f);returnres;}当我在我的代码中使用它时,我指定了模板参数。为什么编译器不能为我推断出这个?我必须如何更改我的模板定义才能使其正常工作?vectornumbers={1,3,5};//vectorstrings=map(numbers,[](intx){return

c++ - -O1/2/3 与 -std=c++1y/11/98 - 如果包含 <cmath> 我收到错误 : '_hypot' was not declared in this scope

我刚刚使用mingw-get-setup更新了MinGW而且我无法构建包含的任何内容header如果我使用大于-O0的东西与-std=c++1y.(我也试过c++11和c++98)我收到这样的错误:g++.exe-pedantic-errors-pedantic-Wextra-Wall-std=c++1y-O3-cZ:\Projects\C++\L6\src\events.cpp-oobj\src\events.oInfileincludedfromz:\lander\mingw\lib\gcc\mingw32\4.8.1\include\c++\cmath:44:0,fromZ:\P

c++ - std::regex 构造函数抛出异常

请注意,这不是StackOverflow上有关gcc的许多问题的重复,我使用的是VisualStudio2013。这个正则表达式的简单构造会抛出std::regex_error:boolcaseInsensitive=true;charpattern[]="\\bword\\b";std::regexre(pattern,std::regex_constants::ECMAScript|(caseInsensitive?std::regex_constants::icase:0));what在异常对象上返回的实际错误是不一致的。通常它是不匹配的括号或括号。为什么?

c++ - std::move(T&&) 和临时对象。临时从哪里来?

我很好奇为什么我不能编译下面的代码。这是无意义的代码(我知道),但我最初在其他一些使用具有完美转发等模板的代码中遇到了这个问题。我设法将问题缩小到std::move/std::forward/std::remove_reference,并且我'我很好奇为什么它首先需要一个临时的...#include#includestructFoo{Foo(Foo&&other){printf("Foo::(Foo&&)%p\n",this);}Foo(){printf("Foo::()%p\n",this);}~Foo(){printf("Foo::~Foo()%p\n",this);}};void

c++ - 关于 std::less 行为的问题

那里发生了什么?#includenamespaceA{structClass{};}booloperator()(A::Class(),A::Class());return0;}这是编译好的。但如果我使用。#include我有错误:g++test.cc-otestInfileincludedfrom/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.2/include/g++-v4/bits/stl_tree.h:64:0,from/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.2/include/g++-v4/set:60,fromlo

c++ - 为什么 `const int ci = 2; std::forward<int>(ci);` 不起作用以及如何修复/解决它?

简单的问题,为什么不thefollowing工作(意味着ci的拷贝)?#includeintmain(){constintci=2;std::forward(ci);}prog.cpp:Infunction'intmain()':prog.cpp:6:23:error:nomatchingfunctionforcallto'forward(constint&)'问题在编写一些模板内容时表现出来,我有一个简单的holder类型,如下所示。为了避免不必要的拷贝,我尽可能使用完美转发,但事实证明这似乎是问题的根源。templatestructholder{Tvalue;holder(T&&v

c++ - 如何实现 std::advance 以更改迭代器类型的行为?

我们对std::advance的了解如下:templatevoidadvance(InputIterator&i,Distancen);目的将迭代器i推进n个元素。如果i是一个随机访问迭代器,函数使用一次operator+或operator-,否则函数重复使用递增或递减运算符(operator++或operator--)直到n个元素被递增。我的问题如下:std::advance是如何实现的,以便识别it是否是随机访问迭代器?它怎么知道它可以使用operator+而不是operator++? 最佳答案 通过iterator_trait

c++ - std::make_array 的当前状态

提议的std::make_array函数的当前状态是什么here?我找不到任何关于它可能被接受的信息。根据cppreference.com,它位于std::experimental命名空间中。C++compilersupport上根本没有提到它也不在Wikipedia-C++17,Wikipedia-C++20,和C++17标准草案。 最佳答案 正如@DeiDei所写,C++17包括templateargumentdeductionforclasses,所以你现在可以写:std::pairp(foo,bar);std::arraya

C++ - vector<> 中的 std::unique_ptr 是 nullptr

我想将Particle对象存储在vector对象中,以便稍后访问它。这些粒子(Electrons和Protons)继承自包含toString()虚方法的Particle类。此toString()方法随后在Electron和Proton类中被覆盖。当我读取vector容器时,我想访问Electron或Proton特定的toString()方法,而不是粒子。显然,一种方法是使用std::unique_ptr。这是我尝试运行的代码的一部分:intmain(){/**/std::vector>particles(nbParticles);particles.push_back(std::uni