草庐IT

c++ - 使用 std::function 时如何解决此 <unresolved overloaded function type> 错误?

在以下(有效的)代码示例中,模板化的register_enum()函数用于迭代枚举并调用用户提供的回调以将枚举值转换为C字符串。所有枚举都在一个类中定义,枚举到字符串的转换是使用静态to_cstring(enum)函数完成的。当一个类(如下面的着色器类)有多个枚举和相应的重载to_cstring(enum)函数时,编译器无法决定将哪个是正确的to_cstring()函数传递给register_enum()。我认为代码比我能解释得更好...#include#include//ActualcodeusesLua,butforsimplification//I'llhideitinthise

c++ - 为什么 range-for 找不到我对 std::istream_iterator 的开始和结束重载?

我有这样的代码std::ifstreamfile(filename,std::ios_base::in);if(file.good()){file.imbue(std::locale(std::locale(),newdelimeter_tokens()));for(auto&entry:std::istream_iterator(file)){std::cout哪里std::istream_iterator的begin()和end()定义如下templatestd::istream_iteratorbegin(std::istream_iterator&stream){returns

c++ - 覆盖 std :exception 的析构函数

如果line8被注释,则以下程序无法在g++4.4中编译。为什么?似乎当我重写std::exception构造函数时,我也必须重写它的析构函数。这是什么原因?#include#includeusingnamespacestd;classA:publicexception{public:A(stringmsg):_msg(msg){}//~A()throw(){};//line8constchar*what()constthrow(){return_msg.c_str();}private:string_msg;};intmain(){}编译错误为:error:looserthrowsp

c++ - 使用 -std=c++11 会破坏二进制兼容性吗?

我一直在努力寻找这个问题-这似乎是一个显而易见的问题-但我还没有找到它:Isamodulecompiledwith"-std=c++11"(g++)binarycompatiblewithmodulesthat没有编译选项?(也就是说,我可以安全地将它们链接在一起吗?)两种编译都将使用完全相同的g++版本。更准确地说,使用gcc4.9.0,我是否可以只在特定的编译单元上使用“-std=c++11”,然后让其他编译器在没有选项的情况下进行编译。 最佳答案 权威引用可以在gcc的C++11ABICompatibility中找到页面。简短

C++11/C++03 和 std::vector 线程安全

我正在阅读有关各种STL容器的线程安全的信息link现在我遇到了仅适用于C++11的这一点Differentelementsinthesamecontainercanbemodifiedconcurrentlybydifferentthreads,exceptfortheelementsofstd::vector(forexample,avectorofstd::futureobjectscanbereceivingvaluesfrommultiplethreads)这是否意味着如果我有一个被多个人使用的方法同时线程(noticethemethoddoesnothaveanylocks

c++ - 使用 std::mutex 关闭头文件的 clr 选项

我有一个VisualStudio项目,其中包含托管代码文件和非托管代码文件。该项目具有CLR支持,但是当我在不需要.NET的地方添加文件时,我只需右键单击该文件即可关闭/crl选项:我添加了一个必须包含非托管代码并使用std::mutex的类。//Foo.hclassFoo{std::mutexm;}编译后出现如下错误:errorC1189:#error:isnotsupportedwhencompilingwith/clror/clr:pure.问题是我没有关闭头文件(.h)的clr的选项,因为这是我右键单击.h文件时的窗口:我该如何解决这个问题? 最佳答

c++ - 浮点负无穷大的 std::exp 为 Visual C++ 2013 中的 x64 构建返回负无穷大

使用std::exp计算e^-infinity在使用无穷大的浮点表示和构建x64二进制文件时返回-infinity使用VisualC++2013。我希望它返回0,这是Win32版本或采用double的std::exp版本的情况。以下构建为x64的代码演示了该问题。#include#includeintmain(constintargc,constchar**argv){std::cout::infinity())::infinity())编译的命令行选项(取自VisualStudio):/GS/Wall/Gy/Zc:wchar_t/Zi/Gm-/Od/sdl/Fd"x64\Releas

c++ - std::unordered_map 指针/引用失效

我有以下代码:std::unordered_mapmap;map["k1"]="v1";auto&v1=map["k1"];map["k2"]="v2";看完http://en.cppreference.com/w/cpp/container/unordered_mapNotesTheswapfunctionsdonotinvalidateanyoftheiteratorsinsidethecontainer,buttheydoinvalidatetheiteratormarkingtheendoftheswapregion.Referencesandpointerstoeitherk

C++ 将 std::function 对象传递给可变参数模板

我想将一个可调用对象(std::function对象)传递给一个类Foo.可调用对象引用另一个具有任意参数的类的成员方法,因此Foo必须是可变参数模板。考虑这段代码:structBar{voidMemberFunction(intx){}};templateclassFoo{public:Foo(std::functionf){}};intmain(){Foom1(&Bar::MemberFunction);return0;}这编译得很好。现在我想写一个工厂函数MakeFoo()返回unique_ptr到Foo对象:templatestd::unique_ptr>MakeFoo(std

c++ - 最令人烦恼的解析阻止了类内初始化 std::vector<int>

这个问题在这里已经有了答案:Whycan'tmemberinitializersuseparentheses?(2个答案)关闭4个月前。C++11允许类内初始化:structFoo{std::vectorv{3};//vectorof3emptystrings};如果我们想在类中初始化一个intsvector,我们会得到其他东西:structFoo{std::vectorv{3};//vectorofoneelementwithvalue3};这个问题好像是语言的限制,asdiscussedinpreviousquestions.但是,如果这不是类内初始化,我们将能够使用圆括号而不是大