草庐IT

char_type

全部标签

c++ - 有没有比 allocator_type 更好的方法来区分可调整大小的容器?

我有operator>>()的模板重载,我需要区分可以调整大小的容器(例如vector)和不能调整大小的容器(例如,数组。我目前只是在使用allocator_type特征(见下面的代码)——它工作得很好——但想知道是否有更明确的测试方法。templatestructis_resizable{typedefuint8_tyes;typedefuint16_tno;templatestaticyestest(classU::allocator_type*);templatestaticnotest(...);staticconstboolvalue=sizeoftest(0)==sizeo

c++ - "unresolved overloaded function type"在 std::function 中有静态函数

尝试将重载静态函数传递给std::function时出现“未解析的重载函数类型”错误。我知道类似的问题,例如this和this.然而,即使那里的答案可以将正确函数的地址放入函数指针中,它们也会因std::function而失败。这是我的MWE:#include#include#includestructClassA{staticstd::stringDoCompress(conststd::string&s){returns;}staticstd::stringDoCompress(constchar*c,size_ts){returnstd::string(c,s);}};voidh

C++ 从 char* 到 char 的无效转换 (char* = *string.begin() )

我有以下代码:std::stringextract(){fstreamopenfile("/home/name/Documents/testfile");std::stringteststring;longlocation=4;longlength=2;teststring.resize(length);char*begin=*teststring.begin();openfile.seekp(location);openfile.read(begin,length);returnteststring;}此代码应该返回在文件中找到的字符串。例如,如果文件的内容是StackOverflo

c++ - 类模板继承在 GCC 中编译失败,但在 Visual Studio 中有效

有人能告诉我为什么以下代码在VisualStudio2010中完美运行,但在gcc5.3中无法编译,尽管它看起来没有任何问题?我已经进行了一些谷歌搜索,但没有找到描述模板类继承的清晰标准方法。#include#includenamespacefoobar{templateclassbasic_foo{public:inlinebasic_foo(){}virtual~basic_foo(){}typedefstd::basic_stringstr_foo;enum{fooEnum=100};};templateclassbasic_bar:privatebasic_foo{public

c++ - C++ "type deduction"和 Haskell "type inference"有什么区别?

在英语语义中,“typededuction”等于“typeinferring”吗?我不确定这只是不同语言设计者选择的成语偏好,或者计算机科学给出了严格的“类型推导”定义,哪个不是“类型推断”?谢谢。 最佳答案 C++规范和工作草案广泛使用“类型推导”来指代没有类型声明作为引用的表达式类型;例如thisworkingdraftonconcepts在谈论auto声明的变量时使用它,我记得很多书在谈论模板时都使用它,那时候我不得不学习——然后忘记了大部分——C++。Typeinference但是,它有自己的维基百科页面,也是编程语言理论中

c++ - printf int 到带有左填充空格的 char 数组

我有一点脑死亡的时刻。我必须将int的字符串表示形式存储到char[]中,但ascii表示形式必须用空格填充。snprintf将完成这项工作。chardata[6];intmsg_len=10;std::snprintf(data,6,"%*d",5,msg_len);//"10"我只是想知道是否有更优雅的方法来做到这一点。我可以访问C++11还有一点问题,我认为snprintf也会添加一个终止符,我必须避免这种情况。我可以有一个中间缓冲区并将其复制到我的数据中,但这会增加额外的复杂性。我需要就地进行,因为这些数据结构是我必须发送到接受这种格式输入的服务器的消息的一部分。消息看起来像:

c++ - 如何在 C++ 中为不同的迭代器 value_types 重载函数

我想在C++11中实现一个带有一对迭代器的模板函数。如果传递了一对迭代器,其值类型是任意类型的std::pair,则实现应该做一些特殊处理。我试图提出以下定义://arbitraryvaluetypestemplatevoidprocess(Iterbegin,Iterend){for(Iteriter=begin;iter!=end;++iter){std::cout::value_type,std::pair>::value>::type*=0>voidprocess(Iterbegin,Iterend){for(Iteriter=begin;iter!=end;++iter){s

c++ - `type *var = (int)0` ,合法与否?

下面的例子:char*var=(int)0;在gcc和cl.exe上编译,但在clang中导致错误:cannotinitializeavariableoftype'char*'withanrvalueoftype'int'谁是正确的?对于它的值(value),C++11说(强调我的)4.10/1Anullpointerconstantisanintegralconstantexpression(5.19)prvalueofintegertypethatevaluatestozerooraprvalueoftypestd::nullptr_t.Anullpointerconstantca

c++ - 将 std::shared_ptr<char> 转换为 std::shared_ptr<unsigned char>

有什么好的方法可以将shared_ptr转换为shared_ptr吗?我想到了以下但它看起来不是很干净。intmain(intargc,char**argv){std::shared_ptrp1=std::make_shared();std::shared_ptrp2=std::shared_ptr(reinterpret_cast(p1.get()),[p1](unsignedchar*){});} 最佳答案 你正在做的事情有一个现成的功能,reinterpret_pointer_cast:std::shared_ptrp2=st

c++ - 从 C++ 中的字符串文字返回 const char*?

这个问题在这里已经有了答案:Howlongdoesastringconstantliveinc++?(1个回答)关闭5年前。通常,我会从函数返回一个std::string,因为返回一个constchar*需要调用者提供一个输出内存缓冲区,而那个缓冲区不可调整大小。但是,如果返回一个constchar*是否有效,如果它来自字符串文字?constchar*generate_c_string(){return"ABC";}这样做(如果有效)可能会更快,因为我不需要动态分配内存来构造std::string。它可能是有效的,因为constchar*x="ABC";是有效的。是否有来自C++标准的