草庐IT

dynamic-typing

全部标签

c++ - 迭代器模式 - 错误 C2679 : binary '<<' : no operator found which takes a right-hand operand of type 'std::string'

这个问题在这里已经有了答案:errorC2679:binary'(1个回答)关闭5年前。我正在尝试使用迭代器模式进行迭代和打印,但出现错误这里是错误:errorC2679:binary'couldbe'std::basic_ostream&std::operator>(std::basic_ostream&,constchar*)'这是错误的来源std::coutgetName();#ifndef_ROBOT1_#define_ROBOT1_namespaceguitars{namespaceComposite{namespaceInventoryParts{usingnamespac

c++ - dynamic_cast 中的类型必须是指向完整类类型的指针或引用,或者 void *

我希望有人能理解为什么下面的代码会失败。我正在尝试从osg::Node*节点对象获取PositionAttitudeTransform(Openscenegraph类)的实例。但是下面是粗体的编译器错误。voidCameraPosCallbackUpdate::operator()(osg::Node*node,osg::NodeVisitor*nv){//othercodegoeshereosg::PositionAttitudeTransform*pat=dynamic_cast(node);}IntelliSense:dynamic_cast中的类型必须是指针或对完整类类型的引用

c++ - 使用 dynamic_cast 而不是传统的多态有什么优势?

我们可以使用多态(继承+虚函数)来泛化不同的类型在一个共同的基类型下,然后引用不同的对象,就好像它们是相同的类型一样。使用dynamic_cast似乎是完全相反的方法,因为本质上我们在决定要采取什么操作之前检查对象的特定类型。是否有任何已知的例子说明某些东西不能像使用dynamic_cast那样容易地使用传统多态性来实现? 最佳答案 每当您发现自己想要基类中的成员函数(如“IsConcreteX”)时(编辑:或者更准确地说,是像“ConcreteX*GetConcreteX”这样的函数),您基本上是在实现自己的dynamic_cas

如何正确地从函数(type_traits)提供向量

我如何返回std::vector由此operator+()?#include#include#include#include#includetemplatestructis_std_vector:std::false_type{};templatestructis_std_vector>:std::true_type{};templatestd::enable_if_t>::value>operator+(T&&vec1,T&&vec2){for(inti=0;i(vec2.at(i)));returnvec1;}intmain(){std::vectorvec1{1,2,3,4,5};std

c++ - 如何在 C++11 中将容器 std::array<type, size> 用于多维数组?

例如,包含三个整数的一维数组可以定义为std::arraymyarray或myarray[3].有没有像std::array这样的容器对于像myarray[3][3]这样的多维数组? 最佳答案 一个关键部分是确保{}初始化工作类似于std::array,并尽可能合理地让自己保持pod状。与std::array的兼容性也很重要,什么比std::array更兼容??所以我的解决方案从std::array中生成多维数组小号:templatestructmulti_array_helper{usingtype=T;};templateusi

c++ - 从 back_insert_iterator 中提取容器的 value_type 的特征类

std::back_insert_iterator的value_type等于void,但它还有一个protected成员container包含指向底层Container的指针。我正在尝试编写一个traits类来提取容器的value_type,如下所示:#include#include#includetemplatestructoutit_vt:OutputIt{usingself_type=outit_vt;usingvalue_type=typenamestd::remove_pointer_t().container)>::value_type;};intmain(){std::v

c++ - "Expected ' (' for function-style cast or type construction"错误是什么意思?

我收到错误“Expected'('forfunction-stylecastortypeconstruction”,我已尽力在线研究此错误的含义,但无法找到导致此错误的任何文档错误。我在StackOverflow上发现的所有相关问题都修复了特定的代码片段,并且没有更笼统地解释导致错误的原因。这些包括Expected'('forfunction-stylecastortypeconstruction答案突出了代码的几个问题。究竟是哪个问题导致了错误尚不清楚。c++Xcodeexpected'('forfunction-stylecastortypeconstruction在主函数中定义函

C++14 元编程 : Automagically build a list of types at compile/init time

使用C++14和CuriouslyRecurringTemplatePattern(CRTP)以及可能的Boost.Hana的某种组合(或boost::mpl如果您愿意),我可以在编译时(或静态初始化时)构建一个类型列表而无需显式声明吗?例如,我有这样的东西(在Coliru上查看):#include#include#includenamespace{structD1{staticconstexprautoval=10;};structD2{staticconstexprautoval=20;};structD3{staticconstexprautoval=30;};}intmain(

c++ - Delphi 中 dynamic_cast 的等价物是什么?

在Delphi中,C++的dynamic_cast、reinterpret_cast和static_cast运算符(尤其是在对象上使用时)的等效项是什么? 最佳答案 重新解释_cast大多数时候,在Delphi中,转换是reinterpret_cast,即一种类型的位和字节被重新解释为另一种类型,例如Integer(myEnum)或Pointer(MyDynamicArrayVar)。有些转换会截掉位,即Integer(MyInt64)会截掉Int64的高32位,而低32位的高位会变成新的符号位。一些Actor阵容扩大,例如Inte

c++ - template<class key, class type> 在 C++ 中的方法之前是什么意思?

我有这段代码,我试图理解遵循的约定,.cpp文件中定义的所有方法都有template写在他们面前。这是什么意思?例子://ConstructortemplateMyOperation::MyOperation(){//methodimplementation}//AmethodtemplateMyOperation::otherOperation(){//methodimplementation}谢谢 最佳答案 必须已经有一个很好的答案,但我也会把我的也扔进池中。C++允许程序结构的声明和实现分开进行。它源于C/C++程序员如何相互