草庐IT

rvalue_reference

全部标签

c++ - "static const int"导致链接错误( undefined reference )

使用以下代码时,我对链接器错误感到困惑://static_const.cpp--completecode#includestructElem{staticconstintvalue=0;};intmain(intargc,char*argv[]){std::vectorv(1);std::vector::iteratorit;it=v.begin();returnit->value;}但是,这在链接时会失败——不知何故,它需要一个符号来表示静态const“值”。$g++static_const.cpp/tmp/ccZTyfe7.o:Infunction`main':static_con

c++ - C++ 类 : undefined reference 中的静态常量

我有一个仅供本地使用的类(即,它的拷贝只是它定义的c++文件)classA{public:staticconstintMY_CONST=5;};voidfun(intb){intj=A::MY_CONST;//noproblemintk=std::min(A::MY_CONST,b);//linkerror://undefinedreferenceto`A::MY_CONST`}所有代码都位于同一个c++文件中。在windows上使用VS编译时,完全没有问题。但是,在Linux上编译时,我只收到第二条语句的undefinedreference错误。有什么建议吗?

c++ - 如何在 C++ 中使用 "add reference"

我是C++新手,有些东西我完全不明白。在C#中,如果我想使用外部库,例如log4net,我只需添加对log4netDLL的引用,并且它的成员对我自动可用(并且在IntelliSense中)。我如何在非托管C++中做到这一点? 最佳答案 除了.dll之外,该库通常还附带1)一个头文件(.h)和2)一个.lib文件。头文件在您的代码中被#include'ed,以便您访问库中的类型和函数声明。.lib链接到您的应用程序(项目属性->链接器->输入,附加依赖项)。.lib文件通常包含自动加载dll并将函数调用转发给它的简单stub。如果您没

c++ - const-reference 限定成员函数

引用限定成员函数的股票示例似乎是这样的:#include#include#include//Easyaccesstoliteralsusingnamespacestd::literals;//FilewrapperclassFile{private://ThewrappedfileFILE*_file;public:File(constchar*name):_file(fopen(name,"r")){//unabletoopenthefile?if(!_file)throwstd::runtime_error{"Unabletoopenfile:"s+name};}~File(){f

c++ - 首先是 std::remove_reference 还是 std::remove_cv?

如果我想提取const引用的类型(如constdouble&中的double),我必须使用:typenamestd::remove_cv::type>::type或typenamestd::remove_reference::type>::type? 最佳答案 首先使用remove_reference。remove_cv仅删除顶级限定符,在引用的情况下,没有任何(或被忽略)。显示差异的示例:#include#includetemplateusingRemove_cv_ref=std::remove_cv::type>;templat

c++ - 优化器 : replace const reference with const object

ChandlerCarruth在他的talk关于编译器优化说编译器在优化具有通过引用传递的参数的函数方面很糟糕。我可以理解当参数是非常量引用时很困难,因为编译器必须处理内存,或者当参数的类型很复杂(一些奇怪的结构或类)时。但是如果参数是const引用和内置类型,真的有什么问题吗?优化器可以用constfloat替换constfloat&吗?当启用SSE指令时,它会更有帮助,因为编译器将能够为它们正确对齐数据。 最佳答案 Canoptimizerreplaceconstfloat&withconstfloat?不,他们不能这样做,因为

c++ - 关于如何识别 Rvalue 或 Lvalue 引用和 if-it-has-a-name 规则

我正在阅读ThomasBecker的article关于右值引用及其使用。在那里,他定义了他所谓的if-it-has-a-name规则:Thingsthataredeclaredasrvaluereferencecanbelvaluesorrvalues.Thedistinguishingcriterionis:ifithasaname,thenitisanlvalue.Otherwise,itisanrvalue.这对我来说听起来很合理。它还清楚地标识了右值引用的右值性。我的问题是:你同意这个规则吗?如果没有,您能否举一个可能违反此规则的示例?如果没有违反这条规则。我们可以使用此规则来

C++基于范围的for循环对valarray rvalue不起作用

我想迭代一个临时的valarray,但它不起作用。这是我的(非工作)代码:#include#includeintmain(){usingnamespacestd;valarraynumerators={99,26,25};valarraydenominators={9,2,5};for(inti:numerators/denominators){cout下面是我想要实现的最小工作示例,除了我不想定义像temp_array这样的对象。#include#includeintmain(){usingnamespacestd;valarraynumerators={99,26,25};vala

c++ - 链接错误 : undefined reference to `vtable for XXX`

这里有一些链接错误。我在网上找了,还是没找到问题。我该如何解决?g++test.cpp-otest/tmp/ccDfCj4N.o:Infunction`Interval::Interval()':test.cpp:(.text._ZN8IntervalC2Ev[Interval::Interval()]+0x9):undefinedreferenceto`vtableforInterval'/tmp/ccDfCj4N.o:Infunction`IntInterval::~IntInterval()':test.cpp:(.text._ZN11IntIntervalD0Ev[IntInt

c++ - 对 google::protobuf::internal::empty_string_[abi:cxx11] 的 undefined reference

我正在尝试使用ProtocolBuffers2.6.1和GNUGCC5.1.0(在Ubuntu14.10上)构建简单的测试应用程序,但出现以下错误:/home/ragnar/cpp-tools/gcc-linux/bin/g++-c"/home/ragnar/cpp-projects/gprotobuf_test/main.cpp"-g-O0-Wall-o./Debug/main.cpp.o-I.-I/home/ragnar/cpp-tools/libs/linux64/protobuf/include-I./home/ragnar/cpp-tools/gcc-linux/bin/g+