草庐IT

render_template_string

全部标签

C++:专门化成员需要 «template<>» 语法

我做错了什么?templateclassBinder{public:staticstd::vector*>all;Node*from;Node*to;Binder(Node*fnode,Node*tonode){from=fnode;to=tonode;Binder::all.push_back(this);}};std::vector*>Binder::all=std::vector*>();//hereitis谢谢。 最佳答案 静态成员的定义被编译器解释为一个特化(实际上,它是一个特化:你给出了一个特定于T=int的声明)。这可

c++ - template-parameter-list of template parameter 是什么意思

引用3.3.9/1中的一句话:Thedeclarativeregionofthenameofatemplateparameterofatemplatetemplate-parameteristhesmallesttemplate-parameter-listinwhichthenamewasintroduced.你能举个例子来理解上面的定义吗?我也想知道模板参数的模板参数列表是什么意思?示例会有所帮助。 最佳答案 template//thedeclarativeregionendshereclassq//hencethenamema

c++ - 如何将 `std::array` 用作 `template<typename> class` 形式的模板参数?

请考虑以下tree类templateclassTuple>classtree{private:Tm_value;Tuplem_children;};templateusingstatic_tree=tree>;定义不明确。std::array不是Tuple的合适模板参数.我假设static_tree的意图清楚了。我们可以做类似的事情templatestructhelper{templateusingtype=std::array;};templateusingstatic_tree=tree::templatetype>;没有helper还有其他选择吗?类(class)?

c++ - 什么是 "template<class T> using owner = T;"?

以下摘自Microsoft的gsl库(https://github.com/microsoft/gsl)的gsl.h:namespacegsl{////GSL.owner:ownershippointers//usingstd::unique_ptr;usingstd::shared_ptr;templateusingowner=T;...};我无法理解以下别名模板的含义:templateusingowner=T;有什么解释吗? 最佳答案 这意味着对于每个T,owner是T的别名. 关于

c++ - 为什么数字类型只有一个 `to_string()`?

我刚刚知道C++有一个std::to_string()在中定义.现在我想知道为什么to_string()仅适用于数字类型。有什么特别的原因,为什么没有更一般的templatestd::stringto_string(constT&t);?可以这样实现:templatestd::stringto_string(constT&t){std::ostringstreams;s我怀疑是这样的将军to_string不存在,因为自己编写很容易,但同样的论点适用于to_string()服用int,double等 最佳答案 因为std::to_st

c++ - 消除 std::vector<std::string> 的列表初始化歧义

我的代码中有一个带有类型签名的重载函数:voidfoo(std::string);voidfoo(std::vector);我希望foo的用户能够使用字符串或字符串列表来调用它//Usecase1foo("str");//Usecase2foo({"str1","str2","str3"});foo({"str1","str2","str3","str4"});问题是当调用者将两个字符串传入foo的初始化列表时。//Problem!foo({"str1","str2"});这个对foo的调用是不明确的,因为它匹配两个类型签名。这是因为显然{"str1","str2"}是std::str

c++ - 创建内联 std::string

是否可以在不创建变量的情况下初始化std::string?我希望完成的事情:throwstd::runtime_error("Error:"+strerror(errno));我目前在做什么:std::stringerror="Error:";std::stringerrmsg(strerror(errno));throwstd::runtime_error(error+errmsg); 最佳答案 只是用其中之一做一个临时的:throwstd::runtime_error(std::string("Error:")+strerror

c++ - typedef a std::string - 最佳实践

我正在用标准C++编写一个库来进行语音转换。到目前为止,我已经使用了std::string。但将来我可能不得不将其更改为其他内容(std::wstring或其他内容)。所以我需要以一种可以轻松切换的方式编写我的库。到目前为止,我已经完成了以下工作。创建了一个将被所有CPP文件使用的头文件为此添加了“typedefstd::string”,并在文件中各处使用了新名称。如果我需要改变类型,我可以简单地在头文件中改变,它会在所有地方反射(reflect)出来。如果有人能看出这是正确的方法或者有更好的方法来做到这一点,我将不胜感激?谢谢 最佳答案

c++ - 如何使用 boost.spirit 提取 std::string?

我尝试使用boost.spirit解析command:param1param2...形式的简单命令行为此我创建了这个解析器:(+(char_-':'))[ref(cmd)=_1]>>':'>>(*char_)[ref(params)=_1]这两个复合解析器的属性类型都是vector,所以如果cmd和params是vector类型就可以了。但是,如果它们是std::string类型,则不会。在网上搜索此解决方案时,我发现提示它也适用于字符串。无论如何我可以用字符串来完成这项工作吗? 最佳答案 当然,当您使用语义操作时,不会发生自动属性

c++ - 为什么 std::cin.getline 没有重载方法来获取 std::string?

我很好奇cin.getline的技术原因和globalgetline功能在不同的地方。不简单地为cin定义所有这些函数签名的动机是什么://THESETWOEXISTistream&cin::getline(char*s,streamsizen);istream&cin::getline(char*s,streamsizen,chardelim);//THESETWOCOULDEXISTistream&cin::getline(string&s);istream&cin::getline(string&s,chardelim);是不是因为可能要添加其他类型,不想把字符串嫁给cin?