Array as template parameter: stack or heap?

My knowledge of the stack as compared with the heap is very rudimentary, but when it comes to arrays, from what I know something like this is created on the stack float x[100]; whereas something like this is created on the heap float* x = new float[100]; But what happens if I create a template array class, and pass it in a "stack" array type (like float[100] )? Example: #include &

数组作为模板参数:堆栈还是堆?

我的堆与堆相比的知识是非常基本的,但是当涉及到数组时,从我知道的东西这样的东西在堆栈上创建 float x[100]; 而像这样的东西是在堆上创建的 float* x = new float[100]; 但是,如果我创建一个模板数组类,并将其传入“堆栈”数组类型(如float[100] ),会发生什么? 例: #include <iostream> using namespace std; template <class T> class Array { public: int size; T* data; Array(int s

Passing a stack allocated argument by reference to an array

I have a function say void theFunc(int num&, int* array) that takes in a an int by reference and an array pointer theFunc(int num&, int* array) { array[0] = num; } this is just an example, so the function does something simple int main() { int k = 3; int* theArray = new int[5]; theFunc(k, theArray); delete[] theArray; return(0); } My question is how is k, a stack allocated

通过引用数组来传递堆栈分配的参数

我有一个函数说无效theFunc(int num&,int *数组),它通过引用接受一个int和一个数组指针 theFunc(int num&, int* array) { array[0] = num; } 这只是一个例子,所以这个功能很简单 int main() { int k = 3; int* theArray = new int[5]; theFunc(k, theArray); delete[] theArray; return(0); } 我的问题是,如何k,堆栈分配int实例,通过引用传递给Func并存储在一个动态存储的数组。 我知道对象/参数不

Pushing a 2d array onto a C++ STL stack?

int test[5][5]; stack<int**> mystack; mystack.push(test); I get the error: no matching function for call to 'std::stack > >::push(int [5][5])' /usr/include/c++/4.4/bits/stl_stack.h:182: note: candidates are: void std::stack<_Tp, _Sequence>::push(const typename _Sequence::value_type&) [with _Tp = int**, _Sequence = std::deque >] I've never really used stack

将2d数组推入C ++ STL堆栈?

int test[5][5]; stack<int**> mystack; mystack.push(test); 我收到错误: 没有匹配函数调用'std :: stack>> :: push(int [5] [5])'/usr/include/c++/4.4/bits/stl_stack.h:182:注意:候选项是:void std :: stack <_Tp,_Sequence> :: push(const typename _Sequence :: value_type&)[with _Tp = int **,_Sequence = std :: deque>] 我从来没有真正使用过堆栈,所以我很感激任何帮助

How is std::string implemented?

我很想知道std :: string是如何实现的,它与c string有什么不同?如果标准没有指定任何实现,那么任何具有解释的实现对于如何满足标准给出的字符串要求都会很好? Virtually every compiler I've used provides source code for the runtime - so whether you're using GCC or MSVC or whatever, you have the capability to look at the implementation. However, a large part or all of std::string will be implemen

std :: string如何实现?

我很想知道std :: string是如何实现的,它与c string有什么不同?如果标准没有指定任何实现,那么任何具有解释的实现对于如何满足标准给出的字符串要求都会很好? 实际上,我使用的每个编译器都为运行时提供了源代码 - 因此,无论您使用的是GCC还是MSVC或其他,都可以查看实现。 但是, std::string大部分或全部将作为模板代码来实现,这可能会导致非常困难的读取。 Scott Meyer的作品Effective STL有一章关于std :: string的

Aliasing a template parameter pack

Pre-Dramatic Hi, maybe this question is a duplicate, but I am relative new to template programming and actually I am not able to find a simple and short solution (-> only finding megabytes of "roxxor-template-magic" which I don't understand) matching my concrete and simple problem and so I feel a little bit lost now. Pre-Information I want to use a "type_container"

别名模板参数包

预戏剧 嗨,也许这个问题是重复的,但我对模板编程相对陌生,实际上我找不到一个简单而简短的解决方案 ( - >只发现兆字节的“roxxor-template-magic”,我不明白)匹配我的具体和简单的问题,所以我现在感觉有点失落。 前信息 我想使用“type_container”作为类的模板参数。 容器是一个简单的结构,它也应该包含一些模板参数包的typedef 。 问题:(与底部的例子有关) 我如何在容器结构中定义和别名类型列表,以及如何

Typedef of a templated alias in C++

I have a the following template class A : template<template<typename>class VectorT> class A { //... } which I instantiate like this: A<MyStdVector> objectA; where MyStdVector is an alias for std::vector with a specific allocator ( MyAllocator ): template<typename T> using MyStdVector = std::vector<T,MyAllocator> I decided to create an alias named Vector in

在C ++中使用模板化别名的Typedef

我有一个以下模板类A : template<template<typename>class VectorT> class A { //... } 我像这样实例化: A<MyStdVector> objectA; MyStdVector是具有特定分配器( MyAllocator )的std::vector的别名: template<typename T> using MyStdVector = std::vector<T,MyAllocator> 我决定在A创建一个名为Vector的别名: template<template<typename>class VectorT> class A {

Variadic template aliases as template arguments (part 2)

This is a follow-up of another question. It refers to the same problem (I hope) but uses an entirely different example to illustrate it. The reason is that in the previous example only experimental GCC 4.9 failed with a compiler error. In this example, also Clang and GCC 4.8.1 fail in different ways: Clang produces an unexpected result and GCC 4.8.1 reports a different error message. Answers

变量模板别名作为模板参数(第2部分)

这是另一个问题的后续。 它指的是同样的问题(我希望),但用一个完全不同的例子来说明它。 原因是在前面的例子中,只有实验GCC 4.9因编译器错误而失败。 在这个例子中,Clang和GCC 4.8.1以不同的方式失败:Clang产生了一个意外的结果,GCC 4.8.1报告了一个不同的错误消息。 上一个问题的答案多多少少说代码是有效的,问题在于GCC的实验版本。 但是这个结果让我更加怀疑。 几个月来我一直困扰着我怀疑是相关(或相同)的

Inferring return type of templated member functions in CRTP

Is it possible to infer the return type of a templated member function in a CRTP base class? While inferring argument types works well, it fails with the return type. Consider the example below. #include <iostream> template <typename Derived> struct base { template <typename R, typename T> R f(T x) { return static_cast<Derived&>(*this).f_impl(x); } };

推断CRTP中的模板成员函数的返回类型

是否有可能推断CRTP基类中的模板成员函数的返回类型? 虽然推断参数类型的效果很好,但是返回类型失败。 考虑下面的例子。 #include <iostream> template <typename Derived> struct base { template <typename R, typename T> R f(T x) { return static_cast<Derived&>(*this).f_impl(x); } }; struct derived : base<derived> { bool f_impl(int x) { std::cout &

aliased templates in nested classes

I am trying to get a template alias for T2<B> starting from an instance of C. template<typename A> struct T1 { template<typename B> struct T2{}; }; template<typename A> class C { T1<A> t; }; template<typename A> using U1=decltype(C<A>::t); template<typename A, typename B> using U2=typename U1<A>::T2<B>; I get a compiler fai

嵌套类中的别名模板

我试图从C的一个实例开始为T2<B>获取一个模板别名。 template<typename A> struct T1 { template<typename B> struct T2{}; }; template<typename A> class C { T1<A> t; }; template<typename A> using U1=decltype(C<A>::t); template<typename A, typename B> using U2=typename U1<A>::T2<B>; 我用gcc 4.8得到编译器失败: gg.cc:18:28: error:

C++ template typedef

I have a class template<size_t N, size_t M> class Matrix { // .... }; I want to make a typedef which creates a Vector (column vector) which is equivalent to a Matrix with sizes N and 1. Something like that: typedef Matrix<N,1> Vector<N>; Which produces compile error. The following creates something similar, but not exactly what I want: template <int N> class Vect

C ++模板typedef

我有一堂课 template<size_t N, size_t M> class Matrix { // .... }; 我想做一个typedef ,它创建一个Vector (列向量),它相当于一个大小为N和1的Matrix 。类似的东西: typedef Matrix<N,1> Vector<N>; 这会产生编译错误。 以下创建类似的东西,但不完全是我想要的: template <int N> class Vector: public Matrix<N,1> { }; 是否有解决方案或不太昂贵的解决方法/最佳实践? C