草庐IT

c++ - 为什么 std::generate 可以在没有命名空间限定符的情况下访问?

这样编译正常正常吗?#include#includeintmain(){std::vectorbuf;generate(buf.begin(),buf.end(),[]{return0;});}(注意generate()前面缺少的std::)是否在某处记录了此行为?还是我偶然发现了编译器或库错误?在我的例子中,Linux上的GCC5.3.0和Clang3.8.0;两者都使用libstdc++,所以可能是库错误? 最佳答案 这是允许的,主要是因为generate的参数在std中。代码如下namespaceFoo{structB{};v

C++ 为什么我的代码不打印对给定文件所做的更新

我试图用C++编写代码,在Linux中执行类似于tail-f的操作。我发现了这个问题:HowtoreadagrowingtextfileinC++?并实现相同。我创建了一个temp.txt并开始执行echo"temp">>temp.txt。但是我的程序没有打印对文件所做的更新。我做错了什么?这是我正在使用的代码#include#include#include#includeintmain(){std::ifstreamifs("temp.txt");if(ifs.is_open()){std::stringline;while(true){while(std::getline(ifs,

c++ - 候选模板被忽略 : substitution failure(error with clang but not g++)

我有一个替换失败的问题,一些类似问题的答案对我没有帮助。代码如下:templateclassReference{public://...templateusingmatrix_t=int[r][c];Reference(constmatrix_t&mat){}};templateclassPartition{//...public://...templateusingmatrix=int[r][c];templatevoidreadPattern(constmatrix&pattern){//...}//...};我这样调用这个模板函数:intmain(){//...constintD

c++ - 模板参数推导 : which compiler is right here?

考虑以下代码:templateclassVector{};#includetemplatevoiddoWork(constVector&,conststd::array&){}intmain(){std::arrayarr;Vectorvec;doWork(vec,arr);}在这里Vector表示在第三方库中定义的类,std::array已知其元素计数为std::size_t.我试过用clang-3.6和g++-5.1编译它。Clang毫无怨言地工作,而g++给出以下错误:test.cpp:Infunction‘intmain()’:test.cpp:17:19:error:noma

c++ - gcc 与 clang、msvc 和 icc : Is this function call ambiguous?

我能得到的所有编译器都同意这很好:templateautofoo(Check,T...)->void;templateautofoo(int,T...)->void;intmain(){foo(7,"");}但是,根据gcc,以下代码(带有不能从函数参数推导的前导模板参数)是不明确的:templateautobar(Check,T...)->void;templateautobar(int,T...)->void;intmain(){bar(7,"");//ambiguousaccordingtogccbar(7);//justfine}另一方面,clang、msvc和icc对此非常满

c++ - decltype(foo(1)) 应该实例化 constexpr 函数模板 foo 吗?

以下代码使用gcc和MSVC编译,但使用clang失败,我使用clang-3.5和当前主干进行了测试。templateconstexprautowrong=false;templateconstexprautofoo(constTt)->int{static_assert(wrong,"");return{};}usingF=decltype(foo(1));intmain(){}clang实例化函数体并偶然发现static_assert。gcc和MSVC只看函数声明,忽略函数体中的static_assert。如果删除constexpr,所有编译器都能正常编译代码。问题:如果声明了返回

c++ - 缩小从 `int`(常量表达式)到 `unsigned int` 的转换 - MSVC vs gcc vs clang

constexprinti=100;structF{F(unsignedint){}};intmain(){F{i};}上面的代码片段:使用-Wall-Wextra-Wpedantic在g++7上编译没有警告。使用-Wall-Wextra-Wpedantic在clang++4上编译没有警告。无法在MSVC2017上编译:conversionfrom'constint'to'unsignedint'requiresanarrowingconversion问:这里MSVC是不是错了?liveexampleongodbolt.orginti=100;structF{F(unsignedint

c++ - 如何告诉 clang 将调试符号放入可执行二进制文件中?

这个问题在这里已经有了答案:Whydoesa2-stagecommand-linebuildwithclangnotgenerateadSYMdirectory?(1个回答)关闭5年前。我的编译命令在macOSSierra中是clang-std=c11-g-Wall-Werror-fsanitize=address-file.c-ofile编译后,它还会生成一个额外的file.dSYM文件,其中包含所有调试符号。但是,当我使用WSL或其他*nix系统时,它不会生成这样的文件,调试符号被嵌入到可执行二进制文件中。所以我只是想知道有没有办法在macOS中使用clang做同样的事情。

c++ - 类模板非依赖成员变量中是否需要模板限定符?

当我在注释行上编译以下代码时,出现编译错误“错误:使用‘模板’关键字将‘foo’视为依赖模板名称”。(测试4)代码的所有其他部分都已成功编译。#includestructmy{templatevoidfoo(){}};voidtest1(){mym;autotr=std::forward_as_tuple(m);auto&t0=std::get(tr);t0.foo();}templatestructtest2{voidmethod(){mym;autotr=std::forward_as_tuple(m);auto&t0=std::get(tr);t0.foo();}};templa

c++ - g++ 和 clang++ 与在模板类中定义的友元模板函数的不同行为

另一个类型的问题“g++和clang++之间谁是正确的?”适用于C++标准专家。下面的代码templatestructfoo{templatefriendvoidbar(){}};intmain(){foof0;foof1;}使用clang++编译没有问题(只有两个“未使用的变量”警告)但给出以下错误tmp_002-11,14,gcc,clang.cpp:Ininstantiationof‘structfoo’:tmp_002-11,14,gcc,clang.cpp:27:12:requiredfromheretmp_002-11,14,gcc,clang.cpp:20:16:erro