我需要扩展 std::basic_string 来处理路径字符串和不同的 operator+:
#include <string>
template <class t_elem, class t_traits, class t_alloc>
class path_basic_string : public std::basic_string<t_elem, t_traits, t_alloc>
{
public:
using base_type = std::basic_string<t_elem, t_traits, t_alloc>;
path_basic_string() = default;
path_basic_string(const path_basic_string & ) = default;
path_basic_string & operator =(const path_basic_string &) = default;
path_basic_string(const base_type & r) :
base_type(r)
{
}
path_basic_string(base_type && r) :
base_type(std::move(r))
{
}
};
using path_string = path_basic_string<char, std::char_traits<char>, std::allocator<char> >;
template <class t_elem, class t_traits, class t_alloc>
inline path_basic_string<t_elem, t_traits, t_alloc> &&
operator +(
path_basic_string<t_elem, t_traits, t_alloc> && l,
std::basic_string<t_elem, t_traits, t_alloc> && r)
{
std::basic_string<t_elem, t_traits, t_alloc> && l_str = std::move(l);
std::basic_string<t_elem, t_traits, t_alloc> && r_str = std::move(r);
const bool has_right = !r_str.empty();
return std::move(
path_basic_string<t_elem, t_traits, t_alloc>{
std::move(std::move(l_str) + (has_right ? "/" : "") + (has_right ? std::move(r_str) : std::move(std::basic_string<t_elem, t_traits, t_alloc>{})))
});
}
template <class t_elem, class t_traits, class t_alloc>
inline path_basic_string<t_elem, t_traits, t_alloc>
operator +(
const path_basic_string<t_elem, t_traits, t_alloc> & l,
const std::basic_string<t_elem, t_traits, t_alloc> & r)
{
const std::basic_string<t_elem, t_traits, t_alloc> & l_str = l;
const bool has_right = !r.empty();
return path_basic_string<t_elem, t_traits, t_alloc>{
l_str + (has_right ? "/" : "") + (has_right ? r : std::basic_string<t_elem, t_traits, t_alloc>{})
};
}
int main()
{
path_string a;
std::string b;
std::string c;
const path_string test = a + (b + c);
return 0;
}
在 https://godbolt.org/z/jhcWoh我有这些错误:
x86 MSVC 19 2015 U3:
/opt/compiler-explorer/windows/19.00.24210/include/xlocale(341): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc <source>(61): error C2666: 'operator +': 3 overloads have similar conversions <source>(44): note: could be 'path_basic_string<char,std::char_traits<char>,std::allocator<char>> operator +<char,std::char_traits<char>,std::allocator<char>>(const path_basic_string<char,std::char_traits<char>,std::allocator<char>> &,const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' <source>(28): note: or 'path_basic_string<char,std::char_traits<char>,std::allocator<char>> &&operator +<char,std::char_traits<char>,std::allocator<char>>(path_basic_string<char,std::char_traits<char>,std::allocator<char>> &&,std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&)' /opt/compiler-explorer/windows/19.00.24210/include/xstring(2310): note: or 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> std::operator +<char,std::char_traits<char>,std::allocator<char>>(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &,const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' /opt/compiler-explorer/windows/19.00.24210/include/xstring(2380): note: or 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> std::operator +<char,std::char_traits<char>,std::allocator<char>>(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &,std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&)' /opt/compiler-explorer/windows/19.00.24210/include/xstring(2390): note: or 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> std::operator +<char,std::char_traits<char>,std::allocator<char>>(std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&,const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' /opt/compiler-explorer/windows/19.00.24210/include/xstring(2400): note: or 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> std::operator +<char,std::char_traits<char>,std::allocator<char>>(std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&,std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&)' <source>(61): note: while trying to match the argument list '(path_string, std::basic_string<char,std::char_traits<char>,std::allocator<char>>)' <source>(61): note: note: qualification adjustment (const/volatile) may be causing the ambiguity Compiler returned: 2
x86-64 gcc 5.4(使用 --std=c++11):
source>: In function 'int main()': <source>:61:40: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: const path_string test = a + (b + c); ^ <source>:44:5: note: candidate 1: path_basic_string<t_elem, t_traits, t_alloc> operator+(const path_basic_string<t_elem, t_traits, t_alloc>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with t_elem = char; t_traits = std::char_traits<char>; t_alloc = std::allocator<char>] operator +( ^ In file included from /opt/compiler-explorer/gcc-5.4.0/include/c++/5.4.0/string:52:0, from <source>:1: /opt/compiler-explorer/gcc-5.4.0/include/c++/5.4.0/bits/basic_string.h:4854:5: note: candidate 2: std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>] operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, ^ Compiler returned: 0
我至少知道一种解决方法。
但到底发生了什么? 荒谬的是我必须再次重载以避免重载碰撞困惑?
更新:
通过从所有 operator+ 的所有 basic_string 类参数中删除 const 和 single reference 来修复。似乎解决了这个问题。
最佳答案
首先,使用 move-from-value 而不是 const& 和 && 重载。
path_basic_string(base_type r) :
base_type(std::move(r))
{
}
并摆脱 base_type const& 构造函数。
其次,使该构造函数显式化:
explicit path_basic_string(base_type r) :
base_type(std::move(r))
{
}
路径与字符串不同。
第三,清理您的 template operator+ 并使其成为 ADL“Koenig”运算符,按值获取其左侧。哦,不要通过右值引用返回任何东西,那是有毒的。
friend path_basic_string
operator +(
path_basic_string l,
base_type const& r)
{
base_type& l_str = l;
if (!r.empty())
l = path_basic_string( std::move(l_str) + "/" + r );
return l;
}
并消除所有噪音。
接下来,从 base_type 继承 ctors。
最后,使用 += 实现追加并使操作对称:
template <class t_elem, class t_traits, class t_alloc>
class path_basic_string : public std::basic_string<t_elem, t_traits, t_alloc>
{
public:
using base_type = std::basic_string<t_elem, t_traits, t_alloc>;
path_basic_string() = default;
path_basic_string(const path_basic_string & ) = default;
path_basic_string & operator =(const path_basic_string &) = default;
using base_type::base_type;
explicit path_basic_string(base_type r) :
base_type(std::move(r))
{
}
path_basic_string& operator+= ( base_type const& rhs ) & {
if (!rhs.empty())
{
base_type& self = *this;
self += '/';
self += rhs;
}
return *this;
}
friend path_basic_string operator+(
base_type l,
base_type const& r
)
{
path_basic_string l_path(std::move(l));
l+=r;
return l;
}
};
operator+ 这里很奇特,因为它只能通过 ADL 找到,但它实际上在类的 base 类型上运行。
这意味着至少有一个参数必须是该类型的实例(或具有该类型的实例作为模板参数)才能找到它。
如果需要,然后转换为基础。
我按值使用 LHS,因为移动一个字符串是便宜到免费的,而且我们需要一个字符串来输出。通过按值获取 LHS 并将其缓冲区(移动后)用作返回值,我们得到了高效的链式加法:
a+b+c+d+e
成为
(a+b)+c+d+e
现在 a+b 的返回值(纯右值)然后用作 (a+b)+c 的 lhs 参数。
缓冲区的回收继续;只创建一个缓冲区(从第一个 +),然后移动、调整大小(希望高效)并重新用于表达式的其余部分。
关于c++ - std::string 类继承和繁琐的 C++ 重载解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53155089/
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我正在使用ruby1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一
简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und
在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
在我的系统中,我已经定义了STI。Dog继承自Animal,在animals表中有一个type列,其值为"Dog"。现在我想让SpecialDog继承自dog,只是为了在某些特殊情况下稍微修改一下行为。数据还是一样。我需要通过SpecialDog运行的所有查询,以返回数据库中类型为Dog的值。我的问题是因为我有一个type列,rails将WHERE"animals"."type"IN('SpecialDog')附加到我的查询中,所以我不能获取原始的Dog条目。所以我想要的是以某种方式覆盖rails在通过SpecialDog访问数据库时使用的值,使其表现得像Dog。有没有办法覆盖用于类型
我正在使用ruby2.1.0我有一个json文件。例如:test.json{"item":[{"apple":1},{"banana":2}]}用YAML.load加载这个文件安全吗?YAML.load(File.read('test.json'))我正在尝试加载一个json或yaml格式的文件。 最佳答案 YAML可以加载JSONYAML.load('{"something":"test","other":4}')=>{"something"=>"test","other"=>4}JSON将无法加载YAML。JSON.load("