草庐IT

forward-reference

全部标签

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++ - 为什么 std::forward 返回 static_cast<T&&> 而不是 static_cast<T>?

让我们有一个名为Y的重载函数:voidY(int&lvalue){cout现在,让我们定义一个类似于std::forward的模板函数templatevoidf(T&&x){Y(static_cast(x));//Usingstatic_cast(x)likeinstd::forward}现在看看main()intmain(){inti=10;f(i);//lvalue>>T=int&f(10);//rvalue>>T=int&&}正如预期的那样,输出是lvalue!rvalue!现在回到模板函数f()并替换static_cast(x)与static_cast(x).让我们看看输出:l

c++ - 使用 std::forward 的构造函数

据我所知,在C++11中高效实现构造函数的两种常用方法是使用其中的两种Foo(constBar&bar):bar_{bar}{};Foo(Bar&&bar):bar_{std::move(bar)}{};或者只是一种Foo(Barbar):bar_{std::move(bar)}{};使用第一个选项可实现最佳性能(例如,希望在左值的情况下为单个拷贝,在右值的情况下希望为单个move),但需要对N个变量进行2N重载,而第二个选项只需要一个函数,代价是传入左值时需要额外move。在大多数情况下,这不会产生太大影响,但肯定这两种选择都不是最佳选择。但是,也可以执行以下操作:templateF

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+

c++ - std::forward_list 和 std::forward_list::push_back

我想使用std::forward_list因为:Forwardlistisacontainerwhichsupportsfastinsertionandremovalofelementsfromanywherefromthecontainer但是没有*std::forward_list::push_back*实现。是否有一种高性能的方式来添加对单或无理由的支持? 最佳答案 我建议反对std::forward_list就像我在几乎所有情况下反对std::list一样。就个人而言,我从来没有在我的代码中发现链表是最好的数据结构。在C++

C++函数参数: use a reference or a pointer (and then dereference)?

我得到了一些代码,其中一些参数是指针,然后指针被取消引用提供值(value)。我担心指针取消引用会花费周期,但是在查看之后上一篇StackOverflow文章:Howexpensiveisittodereferenceapointer?,也许没关系。这里有一些例子:boolMyFunc1(int*val1,int*val2){*val1=5;*val2=10;returntrue;}boolMyFunc2(int&val1,int&val2){val1=5;val2=10;returntrue;}就风格而言,我个人更喜欢通过引用传递,但有一个版本更好(在流程周期方面)比另一个?

c++ - std::forward<T> 和 std::forward<decltype(t)> 有什么区别?

这些功能是否等效?templatevoidfoo(T&&t){bar(std::forward(t));}templatevoidfoo2(T&&t){bar(std::forward(t));}templatevoidfoo3(T&&t){bar(std::forward(t));}如果是,我是否可以始终使用此宏进行完美转发?#defineMY_FORWARD(var)std::forward(var)或者直接使用bar(std::forward(t));我相信foo2和foo3是一样的,但我发现人们总是像foo这样使用向前,是否有任何理由显式编写类型?我了解T和T&&是两种不同的类