草庐IT

此类的 C++ 特征示例

我已经有一段时间没有使用C++的高级功能了,正在刷新我的C++知识。话虽如此,特征和基于策略的编程的概念是我从未真正理解的东西。我想改变它。我正在编写一个通用容器。我想强制执行一个策略,即容器将只存储从特定基类派生的类。这是因为当尝试访问vector边界之外的项目时,容器会返回一个无效对象(而不是抛出)。templateclassGenericContainer{private:typedefstd::vectorTypeVect;voidaddElement(constT&elem);TypeVectm_elems;public:unsignedintsize()const;T&el

c++ - 为什么 cppreference 将 type_traits xxx_v 快捷方式定义为内联 constexpr 而不仅仅是 constexpr?

为什么cppreference将type_traitsxxx_v快捷方式定义为inlineconstexpr而不仅仅是constexpr?例如,参见is_integral_v:templateinlineconstexprboolis_integral_v=is_integral::value;这只是风格问题还是行为上有一些差异?据我所知constexpr变量是隐式inline.编辑:查看最新标准的草案,它也使用inlineconstexpr。那么这个问题实际上适用于标准。 最佳答案 [dcl.constexpr]/9Aconste

c++ - 为什么 cppreference 将 type_traits xxx_v 快捷方式定义为内联 constexpr 而不仅仅是 constexpr?

为什么cppreference将type_traitsxxx_v快捷方式定义为inlineconstexpr而不仅仅是constexpr?例如,参见is_integral_v:templateinlineconstexprboolis_integral_v=is_integral::value;这只是风格问题还是行为上有一些差异?据我所知constexpr变量是隐式inline.编辑:查看最新标准的草案,它也使用inlineconstexpr。那么这个问题实际上适用于标准。 最佳答案 [dcl.constexpr]/9Aconste

解决 undefined reference to cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>,....

在使用opencv时候可能会遇到undefinedreferencetocv::imread(std::__cxx11::basic_string,std::allocator>const&,int)'其主旨原因是使用的函数版本和引用的库函数版本不一至,要确保使用的函数和引用的库函数版本一致。1、如何知道函数版本和引用的是否一样1.1使用nm对目标文件进行分析生成.o文件g++-E-I/usr/local/include/opencv4/-L/usr/local/lib-lopencv_highgui-lopencv_imgcodecs-lopencv_imgproc-lopencv_core

c++ - allocator_traits::construct() 与 allocator_traits::allocate()

C++11提供了std::allocator_traits类作为使用分配器的标准方式。静态函数std::allocator_traits::construct()将一个指针指向应该构造对象的位置。然而,std::allocator_traits::allocate()静态函数返回一个allocator::pointer值,它只需要表现得像一个指针,但不一定一个(一般来说,虽然std::allocator::pointer需要是一个指针)。如果分配和构造静态方法通常会与不兼容的类型一起工作,那么应该如何使用它们?只有当pointer类型实际上可以转换为普通指针时才能使用它们吗?

c++ - iterator_traits SFINAE 友好性

阅读excerpt时来自cppreferenceIfIteratordoesnothavethefivemembertypesdifference_type,value_type,pointer,reference,anditerator_category,thenthistemplatehasnomembersbyanyofthosenames(std::iterator_traitsisSFINAE-friendly)我自然而然地认为这意味着每个成员类型在迭代器本身中定义时就被定义了。但是你瞧,这实际上意味着如果定义了所有五个,那么它们就被定义了。structdefined{usi

c++ - GNU/G++ 4.9(2.95.3 之前的版本)的 C++11 中的 string_char_traits<char>

我有一些遗留的C++代码(用于使用GNUg++2.95.3进行编译)具有以下声明std::basic_string,malloc_alloc>x;头文件是#include现在,我正在迁移到GUg++4.9,但出现此错误:1.std/bastring.h未找到2.当我改变#include作为#include,我收到以下错误:error:'string_char_traits'wasnotdeclaredinthisscopestd::basic_string,malloc_alloc>x;error:templateargument2isinvalidstd::basic_string,

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