草庐IT

auto-format

全部标签

c++ - 编写 c++ 函数 format_string 进行格式化,如 std::string 的 sprintf

为了方便使用,我想编写类似于sprintf的格式化函数,只返回std::string,如下所示:std::stringformat_string(constchar*format,...)我可以在那里使用vsnprintf但有问题-我事先不知道临时缓冲区应该有多长。Microsoft上有函数_vscprintf可以做到这一点,但我认为它不可移植?一个选项是让临时缓冲区开始一些已知的大小,然后如果发现它不够用vsnprintf增加它。有更好的方法吗?谢谢附言请在没有提升的情况下给出答案。我知道Boost,但我很好奇如何在没有Boost的情况下实现它。 最佳答案

c++ - auto stdMaxInt = std::max<int> 的类型推导失败;

使用GCC4.8.4和g++--std=c++11main.cpp输出以下errorerror:unabletodeduce‘auto’from‘max’autostdMaxInt=std::max;对于这段代码#includetemplateconstT&myMax(constT&a,constT&b){return(a;myMaxInt(1,2);autostdMaxInt=std::max;stdMaxInt(1,2);}为什么它适用于myMax但不适用于std::max?我们可以让它与std::max一起工作吗? 最佳答案

c++ - constexpr if with initializer 由标准保证吗? 'constexpr(constexpr auto x = f(); x) { }'

我找不到任何关于新C++17if初始化语法的信息和“constexprif”在:http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0128r1.html不过,Clang-HEAD支持该语法...constexprautof(){returntrue;}intmain(){ifconstexpr(constexprautox=f();x){}}在线代码在这里->http://melpon.org/wandbox/permlink/dj3a9ChvjhlNc8nr是constexprif带有标准保证的初始值设定项,如constexpr

c++ - 错误 : ‘template<class> class std::auto_ptr’ is deprecated

我正在使用scons和ubuntu。当我使用'scons'制作一些程序时,会发生错误,例如,src/db/DBTextLoader.cc:296:3:error:‘templateclassstd::auto_ptr’isdeprecated[-Werror=deprecated-declarations]/usr/include/c++/5/bits/unique_ptr.h:49:28:note:declaredheretemplateclassauto_ptr;这是我的命令;$./configuer$sourcesomething.sh$scons其实我也不知道。我已经在搜索这个

c++ - Qt - 获取 "warning: format not a string literal and no format arguments"

在这样的行上不断收到警告qDebug("Anerroroccuredwhiletryingtocreatefolder"+workdir.toAscii());workdir是QString()warning:formatnotastringliteralandnoformatarguments 最佳答案 大概应该是:qDebug("Anerroroccuredwhiletryingtocreatefolder%s",workdir.constData());自qDebug将constchar*作为第一个参数。

c++ - 有没有办法强制使用 "this->"用于 clang-format/clang-tidy 中的类成员/方法?

我到处搜索,但我可能用错了术语。我还没有为此找到选项。我唯一发现的是这个未回答的问题(但是有点宽泛):CPPlint:Canyouenforceuseofthisforclassvariables?. 最佳答案 鉴于existingoptions,我不相信这在clang-format中是可能的,future也不会。主要原因是程序的工作方式。它不会将C++代码解析为AST,而是将文本标记化而不需要包含(定义它的成员和全局变量)而不是编译数据库(影响定义、包含路径……)它是甚至可以给它一段代码并重新格式化。从问题的性质来看,如果它可以存

c++ - 自定义支持 __attribute__((format))

GCC和Clang都支持对可变参数函数(如printf)进行编译时检查。这些编译器接受如下语法:externvoiddprintf(intdlevel,constchar*format,...)__attribute__((format(printf,2,3)));/*2=format3=params*/在OSX上,Cocoa框架还使用了NSString的扩展:#defineNS_FORMAT_FUNCTION(F,A)__attribute__((format(__NSString__,F,A)))在我们公司,我们有一个自定义的C++框架,其中包含一堆类,例如BaseString,它

c++ - 遍历动态 vector 时 auto 的异常行为

我正在使用自动遍历vector(附加代码)。在遍历的同时,我还在后面附加了一些元素。我没想到会得到这样的输出。#include#includeusingnamespacestd;vectordynamic_vector;voidaccess(){for(autoi:dynamic_vector){if(i==3){dynamic_vector.push_back(4);dynamic_vector.push_back(5);}cout输出:123我原以为从1到5的所有数字都会被打印出来。我无法理解如何使用auto进行遍历? 最佳答案

c++ - lambdas 中的模板参数列表中的 auto 是标准的一部分吗?

今天,我偶然发现了以下代码片段:#includeintmain(){autoa=[](std::pairvalue){};a(std::pair{3,true});}http://cpp.sh/5p34我只有一个问题:标准支持这段代码吗?它在GCC中编译(使用-std=c++14),但不是clang或VisualStudio2015(VC++14)。这似乎应该成为标准的一部分,因为如果lambda应该具有与常规函数相同的模板支持,那么应该支持它。这似乎可以转换为所有模板类型,而不仅仅是std::pair。 最佳答案 在C++14中,

c++ - const auto 和 auto const 如何应用于指针?

我尝试了一些代码,想知道在使用auto时C++中的const限定符如何应用于指针类型。intmain(){intfoo=1;intbar=2;//Expected:constint*ptr_to_const_int=&foo;constautoptr_to_const_int=&foo;//Expected:int*constconst_ptr_to_int=&foo;autoconstconst_ptr_to_int=&foo;*ptr_to_const_int=3;//Thoughtthiswoulderror//ptr_to_const_int=&bar;Thisdoeserro