我的基本想法是从std :: tuple派生我自己的类,以获得一些内部帮助器类型:
template <typename ... T>
class TypeContainer: public std::tuple<T...>
{
public:
using BaseType = std::tuple<T...>;
static const size_t Size = sizeof...(T);
TypeContainer(T... args):std::tuple<T...>(args...){};
using index_sequence = std::index_sequence_for<T...>;
};
Run Code Online (Sandbox Code Playgroud)
现在我尝试使用如下代码:
using MyType_tuple_with_empty = std::tuple< std::tuple<float,int>, std::tuple<>, std::tuple<int>>;
using MyType_typecontainer_with_empty = TypeContainer< TypeContainer<float,int>, TypeContainer<>, TypeContainer<int>>;
using MyType_tuple_non_empty = std::tuple< std::tuple<float,int>, std::tuple<int>, std::tuple<int>>;
using MyType_typecontainer_non_empty = TypeContainer< TypeContainer<float,int>, TypeContainer<int>, TypeContainer<int>>;
template <typename T>
void Do( const T& parms )
{
// The following lines result in errors if TypeContainer with
// …Run Code Online (Sandbox Code Playgroud) 对于用户定义的字符串文字,如果我使用以下形式的定义,则给定的字符串保证null终止吗?我知道第二个参数计数给出的大小没有任何终止,如果有的话.
void operator"" _x( const char* n, size_t s)
{
std::cout << "String: " << s << " Len: " << s << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
如果我使用这个版本的定义,我看到没有空终止字符!
template <class T, T... Chrs>
void operator""_s()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
Run Code Online (Sandbox Code Playgroud) 如果我像下面的例子那样使用一系列继承,我可以使用来自最深基础的变量而没有任何问题:
class A { public: int x; };
class B : public A { };
class C: public B { public: void Do() { cout << x << endl; } };
Run Code Online (Sandbox Code Playgroud)
如果我对递归可变参数模板类执行相同操作,则无法访问我的变量.知道如何访问变量以及为什么我看不到我的变量?
template <class ...Parms>
class Example;
template <class Head, class ...Parms>
class Example<Head, Parms...>: public Example<Parms...>
{
};
template <>
class Example<>
{
public:
int x;
};
template <class ...Parms>
class Last: public Example<Parms...>
{
void Do() { cout << x << endl; }
};
Run Code Online (Sandbox Code Playgroud)
在实例化任何类的实例之前编译失败!
我有可变参数模板模板的问题:
template <typename T> class A { };
template< template <typename> class T> class B { };
template <template <typename> class T, typename parm> class C { typedef T<parm> type; };
template <typename... types> class D { };
template <template <typename...> class T, typename ... parms> class E { typedef T<parms...> type; };
// How to pass list in list??
template < template <typename...> class ...T, ???>
class F
{
};
Run Code Online (Sandbox Code Playgroud)
首先,将一个类型传递给模板,没问题:
A<int> a; //ok
Run Code Online (Sandbox Code Playgroud)
现在,我想从B创建一个实例,但无法传递模板模板参数:
B<A> b; // ok, …Run Code Online (Sandbox Code Playgroud) 如果尝试对进行统一的初始化,则会得到不同的结果std::set。
例:
int main()
{
std::array a {1,2,3,4};
std::set<int> s1 {a.begin(), a.end()};
std::set s2 {a.begin(), a.end()};
std::set s3 (a.begin(), a.end());
for(auto& i: s1) { std::cout << i << "\n"; }
std::cout << "####" << std::endl;
for(auto& i: s2) { std::cout << i << "\n"; }
std::cout << "####" << std::endl;
for(auto& i: s3) { std::cout << i << "\n"; }
}
Run Code Online (Sandbox Code Playgroud)
结果是:
1
2
3
4
####
0x7ffecf9d12e0
0x7ffecf9d12f0
####
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
这似乎与“演绎指南”有关,如果与{}或() …
我在使用常量初始化类时遇到了麻烦:
为什么使用指向同一类中成员的指针初始化会导致错误?出现错误而不使用"使用"类!
class A
{
private:
int a;
const int* const aptr;
public:
constexpr A( int _a):
a(_a)
, aptr( &a) // why aptr could not be initialized?
{}
};
class Data { } d1;
class B
{
private:
Data* dptr1;
public:
constexpr B(Data* _p): dptr1( _p) {}
};
class Use
{
static constexpr A a{2}; // fail! error: field initializer is not constant
static constexpr B b{&d1}; // works
};
Run Code Online (Sandbox Code Playgroud) 我尝试为avr c ++构建构建一个小测试用例集.
通常从c ++库提供一些"特殊功能".现在我想写一个测试程序,它产生这个必须链接的错误代码__cxa_deleted_virtual.
任何人都可以提供一个代码片段,导致链接到该功能?
我实际上不知道如何制作这个"错误"代码.
一个类可以包含一个必须是静态的成员模板变量:
class B
{
public:
template <typename X>
static X var;
B() { std::cout << "Create B " << __PRETTY_FUNCTION__ << std::endl; }
template <typename T>
void Print() { std::cout << "Value is " << var<T> << std::endl; }
};
Run Code Online (Sandbox Code Playgroud)
它必须在类范围之外声明所有静态成员:
以下编译并按预期工作:
template<typename T> T B::var=9; // makes only sense for int,float,double...
Run Code Online (Sandbox Code Playgroud)
但是如何将这样的var专门化为以下非工作代码(使用gcc 6.1的错误消息):
template <> double B::var<double>=1.123;
Run Code Online (Sandbox Code Playgroud)
失败:
main.cpp:49:23: error: parse error in template argument list
template <> double B::var<double>= 1.123;
^~~~~~~~~~~~~~~~~~
main.cpp:49:23: error: template argument 1 is invalid …Run Code Online (Sandbox Code Playgroud) c++ templates template-specialization c++14 template-variables
使用std::visit/ std::variant我在分析器输出中看到std::__detail::__variant::__gen_vtable_impl函数占用的时间最多.
我做了这样的测试:
// 3 class families, all like this
class ElementDerivedN: public ElementBase
{
...
std::variant<ElementDerived1*, ElementDerived2*,... > GetVariant() override { return this; }
}
std::vector<Element*> elements;
std::vector<Visitor*> visitors;
std::vector<Third*> thirds;
// prepare a hack to get dynamic function object:
template<class... Ts> struct funcs : Ts... { using Ts::operator()...; };
template<class... Ts> funcs(Ts...) -> funcs<Ts...>;
// demo functions:
struct Actions { template < typename R, typename S, typename T> void operator()( R*, S*, T* …Run Code Online (Sandbox Code Playgroud) 我开始尝试std::ranges并想了解视图是如何真正起作用的。所以我尝试编写自己的容器和迭代器类型,并希望在视图中使用它。
但是似乎缺少一些东西,但编译器只告诉我begin()视图中没有方法,但没有告诉我为什么。
例子:
#include <iostream>
#include <array>
#include <ranges>
class MyFixedContainer;
class MyIterator
{
MyFixedContainer* ptr;
unsigned int offset;
public:
MyIterator( MyFixedContainer* ptr_, unsigned int offset_ ): ptr{ ptr_},offset{offset_}{}
bool operator==( MyIterator& other ) const
{
return ( ptr == other.ptr )&& ( offset == other.offset );
}
bool operator!=( MyIterator& other ) const
{
return !(*this == other);
}
MyIterator operator++()
{
offset++;
return *this;
}
MyIterator operator++(int)
{
MyIterator tmp = *this;
offset++;
return …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++11 ×3
libstdc++ ×2
templates ×2
c++14 ×1
c++17 ×1
c++20 ×1
constexpr ×1
inheritance ×1
optimization ×1
std-ranges ×1
stdtuple ×1
tuples ×1
variant ×1
visitor ×1