jjzjj

c++ - 使用类型删除创建运行时 type_traits 查询

是否有可能使用类型删除来创建封装任意类型的对象(我们称之为ErasedType),并且可以在运行时查询以判断是否存在另一个任意类型T可转换为ErasedType?考虑之后,我不认为这是可能的-尽管看起来它在理论上可能是可能的。编译器会知道哪些类型T我们正在尝试与ErasedType进行比较,因此可以在运行前生成必要的代码。问题是,在实践中,似乎没有任何方法可以将模板参数类型从基类实例传递到子类实例。例如:structFooBase{templateboolis_convertible(){returncall_derived();}protected:virtualboolcall_d

c++ - boost type_traits is_array

我一直在尝试浏览Boosttype-traitsheader,考虑到无数#define提供的强烈不可读性,现在我感到非常恶心。然后是更多#define。具体来说,我有兴趣弄清楚以下3个特征:类型T是数组、类还是枚举。任何人都可以帮助建议一些破译明显疯狂背后的方法的方法吗?比如你如何从一个类型中找出特征背后的理论,任何相关的阅读Material等。 最佳答案 is_array非常简单直接:templatestructis_array{staticconstboolvalue=false;};templatestructis_array

c++ - std::allocator_traits::construct with const 指针

下面的代码可以正常编译:#include#includeintmain(){constint*a=newint(5);std::cout>;autoalloc=std::allocator();at::construct(alloc,a);std::cout在libstdc++的背后::new((void*)a)int;但是a是const!这是未定义的行为吗?或者placementnew不算修改?我修改了*a的值,是const。据我了解,这是不允许的:Modifyingaconstobjectthroughanon-constaccesspathandreferringtoavolat

c++ - type_traits - 连续内存

我有一个处理任何容器类型的接口(interface)。std::vector、std::array,甚至std::basic_string。问题是没有什么可以阻止某人传递没有连续内存的容器。我目前的解决方案是删除那些我想阻止的接口(interface)。voiddosoemthing(conststd::list&)=delete;voiddosoemthing(conststd::map&)=delete;但是,如果我可以只添加一个基于类型特征的静态断言,我会更愿意。这引出了我的问题。它们是否存在可用于识别其内存是否连续的容器类型特征?我一直在通过documentation来还没有找

c++ - 没有匹配函数调用 `std::basic_ofstream<char, std::char_traits<char>>::basic_ofstream(std::string&)'

我正在尝试编写一个程序,要求用户输入文件名,然后打开该文件。当我编译它时,出现以下错误:nomatchingfunctionforcalltostd::basic_ofstream>::basic_ofstream(std::string&)这是我的代码:usingnamespacestd;intmain(){stringasegurado;cout>asegurado;ofstreamentrada(asegurado,"");if(entrada.fail()){cout 最佳答案 std::ofstream如果您有C++11或

具有相同名称的 Php Class 和 Trait 方法

我有这种特殊情况,我的特征有一个方法,我的类有一个方法,两者同名。我需要使用两种方法(来自特征和类的方法)在包含相同方法的类中namespaceSome\Namespace;useSome\Other\Namespace\TestTrait;classTestClass{useTestTrait;publicfunctionindex(){//shouldcallthemethodfromtheclass$this->getId();//shouldalsocallthemethodfromthetrait$this->getId();}privatefunctiongetId(){/

php - 交响乐 : is there a best practice about the directories' name including traits and interfaces?

我想我无法在我的包中创建Trait或Interface目录,因为这是一个在使用时会带来问题的关键字(即useMyBundle\trait\MyTrait).是否有关于如何命名这些目录的最佳实践? 最佳答案 SymfonybestpractisesSymfony-OrganizingYourBusinessLogicPSRcodingstandards特定于Symfony:将您的特征放在Util文件夹中。在接口(interface)方面;例如你有一个名为Hello和HelloInterface的服务类,那么它们都应该转到Service

php - PHP 中的 Traits 有哪些可能的使用场景?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:traitsinphp–anyrealworldexamples/bestpractices?在什么样的情况下会用到PHP中的Traits?我对此有一个很好的整体想法,但我似乎想不出在我编写的应用程序中使用它们的方法,但这可能是因为它当时不需要特征。我已经意识到需要特征的一个场景:事件。与其拥有一个实现观察者模式的类并让所有其他类继承它,不如将其设为特征并让想要触发事件或订阅的类使用该特征。例如,Yii框架通过在CComponent类中实现内容而不是使用Trait来做错。基本上可以在类之间共享的功能,但可能沿

php - Doctrine 2 : Is there a way to inherit mapping from a trait using yaml or xml?

我找到了followingexampleinthedoctrinedocumentation他们在特征中添加了映射:/***Traitclass*/traitExampleTrait{/**@Id@Column(type="string")*/private$id;/***@Column(name="trait_foo",type="integer",length=100,nullable=true,unique=true)*/protected$foo;/***@OneToOne(targetEntity="Bar",cascade={"persist","merge"})*@Joi

c++ - 如何在类范围内定义/专门化 type_trait?

我有以下情况:我的问题围绕使用强类型枚举类作为标志(就像在C#中使用Flags-Attribute一样)。我知道这不是首先要使用枚举类的方式,但这不是这个问题的重点。我已经定义了几个用于这些枚举类的运算符和函数,以及一个自定义类型特征来区分普通枚举和标志枚举。这是一个例子://Defaulttype_traitwhichdisablesthefollowingoperatorstemplatestructis_flags:std::false_type{};//Exampleoperatortouseenumclassasflagstemplatestd::enable_if_t::v