草庐IT

Duck-typing

全部标签

c++ - Clang 问题 : implicit type conversion at construction time

概要我正在努力使C++11代码与Clang兼容,并遇到了GCC>=4.6接受代码而Clang>=3.1不接受的情况。Clang认为候选构造函数不可行。详情这里是一个精简的例子来说明这个问题:#includetemplatestructT;templatestructT{typedefTsuper;constexprT(){}templateT(Args&&...){}};templatestructT:T{typedefTsuper;Headhead;T(Headarg):super(),head(std::move(arg)){}};structvoid_type{constexpr

c++ - std::type_info::hash_code() 的唯一性和 "should"的含义

是否意味着要保证相同的std::type_info::hash_code()值表示相同的类型?Cplusplus.com似乎是这么说的:Thisfunctionreturnsthesamevalueforanytwotype_infoobjectsthatcompareequal,anddifferentvaluesfordistincttypesthatdonot.[Emphasismine]Cppreference似乎另有说法:Returnsanunspecifiedvalue,whichisidenticalforobjects,referringtothesametype.No

c++ - 在 OSX 10.9.1 上使用 g++ 编译时出错 : unknown type name '__darwin_wctype_t'

在OSX10.9.1上使用g++从命令行编译基本代码(我附加了一个helloworld作为示例)时#includeintmain(){std::cout我用命令编译:g++hello.cc-ohw这会生成以下错误消息:Infileincludedfromhello.cc:1:Infileincludedfrom/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:38:Infileincludedfrom/Applicat

C++11 "enumerated types"(17.5.2.1.2)

引自C++11标准(17.5.2.1.2枚举类型):1SeveraltypesdefinedinClause27areenumeratedtypes.Eachenumeratedtypemaybeimplementedasanenumerationorasasynonymforanenumeration(Suchasanintegertype,withconstantintegervalues(3.9.1)).2Theenumeratedtypeenumeratedcanbewritten:enumenumerated{V0,V1,V2,V3,.....};staticconstenu

C++ 函数模板 : Derived and explicit return type

我有以下问题,我只是没有找到合适的解决方案(也许没有):我有一个模板化方法,其中返回类型取决于输入类型,感谢C++11decltype返回类型可以很容易地导出,但是如果需要,我还希望允许用户明确定义返回类型。更正式地说,我有一个模板化函数f,我想被调用为f(x),既没有显式定义输入类型,也没有显式定义返回类型。我也希望能够将其称为fx()明确定义返回类型,但输入类型仍然自动派生。现在,用C++11满足第一个约束很容易(假设还有另一个模板化方法:templateautof(constInT&in)->decltype(/*codederivingthereturntypeusingin*

c++ - type、value_type 和 element_type 之间有什么区别,何时使用它们?

我已经编写了一个模板类,它应该公开其模板参数,但我不确定合适的命名。我已经为本质上相同的东西找到了三个不同的名称(据我所知):容器,例如std::vector使用value_type智能指针,例如std::unique_ptr使用element_typestd::reference_wrapper仅使用type这些不同名称背后的想法是什么?哪些标准算法或特征类取决于哪个名称?我应该为我的类使用哪个名称(介于智能指针和引用包装器之间的名称)? 最佳答案 value_type是容器(以及迭代器等)的成员类型。它提供对象的类型,通常是一个

c++ - typeid 何时可以为同一类型返回不同的 type_info 实例?

AndreiAlexandrescu写入ModernC++Design:Theobjectsreturnedbytypeidhavestaticstorage,soyoudon'thavetoworryaboutlifetimeissues.安德烈继续说道:Thestandarddoesnotguaranteethateachinvocationof,say,typeid(int)returnsareferencetothesametype_infoobject.尽管标准不保证这一点,但在GCC和VisualStudio等常见编译器中如何实现这一点?假设typeid没有泄漏(并且每次调

c++ - "temporary of type ' A ' has protected destructor", 但它的类型是 B

在以下代码中,使用Clang8.0.0+和-std=c++17编译,使用B{}创建派生类实例会报错错误:'A'类型的临时对象具有protected析构函数。当临时文件的类型为B(因此应该有一个公共(public)析构函数)时,为什么A会出现在此消息中?https://godbolt.org/z/uOzwYaclassA{protected:A()=default;~A()=default;};classB:publicA{//canalsoomitthese3lineswiththesameresultpublic:B()=default;~B()=default;};voidfoo(

c++ - VC++ 2010 中的 "moveable-only types"问题

我最近安装了VisualStudio2010ProfessionalRC来试用它并测试在VC++2010中实现的几个C++0x功能。我实例化了std::unique_ptr的std::vector,没有任何问题。但是,当我尝试通过将临时对象传递给push_back来填充它时,编译器会提示unique_ptr的复制构造函数是私有(private)的。我尝试通过移动它来插入一个左值,它工作得很好。#include#includeintmain(){typedefstd::unique_ptrint_ptr;int_ptrpi(newint(1));std::vectorvec;vec.pu

c++ - 什么时候将 node_type 与 std::map::insert 一起使用?

我已经习惯了std::map的现有接口(interface)。插入元素返回一个描述插入成功的bool值,以及关于插入元素所在位置的迭代器。templatestd::pairinsert(P&&value);//(sinceC++11)C++17添加了类似的调用,但类型名称不同:insert_return_typeinsert(node_type&&nh);//(sinceC++17)我试着查找什么是node_type,但它主要是未指定的:templateclass/*unspecified*/为什么要在C++17中添加此函数,我什么时候可以在旧调用上使用它?