Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem on non-type template parameter.

shuisheng
Guest
 
Posts: n/a
#1: Sep 27 '06
Dear All,

I am wondering how to use the following template

template<class _T, size_t * _p>
class A
{
};

Anyone can give me an instance. For your reference, please see
http://msdn2.microsoft.com/en-us/library/5w0xdk8f.aspx

and also please see if the following is right? (from
http://msdn2.microsoft.com/en-us/library/fhfd9502.aspx )

// C2970.cpp
// compile with: /c
static int si;
// could declare nonstatic to resolve all errors << -- I think
this is WRONG !!!
// int si;

template <int i>
class X {};

template <int *pi>
class Y {};

X<sianX; // C2970 cannot use static variable in templates

// this would also work
const int i = 10;
X<ianX2;


Thanks,

Shuisheng


Victor Bazarov
Guest
 
Posts: n/a
#2: Sep 27 '06

re: Problem on non-type template parameter.


shuisheng wrote:
Quote:
I am wondering how to use the following template
>
template<class _T, size_t * _p>
class A
{
};
How to use it for what? It seems rather useless.
Quote:
Anyone can give me an instance. For your reference, please see
http://msdn2.microsoft.com/en-us/library/5w0xdk8f.aspx
How is it relevant? Please elaborate. Better yet, don't forward
the readers of your message to Microsoft, post your own code.
Quote:
and also please see if the following is right? (from
http://msdn2.microsoft.com/en-us/library/fhfd9502.aspx )
>
// C2970.cpp
// compile with: /c
static int si;
// could declare nonstatic to resolve all errors << -- I think
this is WRONG !!!
// int si;
>
template <int i>
class X {};
>
template <int *pi>
class Y {};
>
X<sianX; // C2970 cannot use static variable in templates
A non-template integral argument requires compile-time constant.
Quote:
// this would also work
const int i = 10;
X<ianX2;
Yes, here it's fine.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


shuisheng
Guest
 
Posts: n/a
#3: Sep 27 '06

re: Problem on non-type template parameter.



Victor Bazarov 写道:
Quote:
shuisheng wrote:
Quote:
I am wondering how to use the following template

template<class _T, size_t * _p>
class A
{
};
>
How to use it for what? It seems rather useless.
Here is the motivation.

If I have a template like

template<class _T, size_t nDim, size_t *p>
class TinyVector
{ };

Here nDim is number of dimensions of the array, p saves the size of the
array. This template will have lots of uses for my project.

such as p = {3, 4, 5}, nDim = 3, _T = double, I defines a 3D Array with
given size 3 * 4 * 5.

Thanks,

Shuisheng

Victor Bazarov
Guest
 
Posts: n/a
#4: Sep 27 '06

re: Problem on non-type template parameter.


shuisheng wrote:
Quote:
Victor Bazarov ??:
>
Quote:
>shuisheng wrote:
Quote:
>>I am wondering how to use the following template
>>>
>>template<class _T, size_t * _p>
>>class A
>>{
>>};
>>
>How to use it for what? It seems rather useless.
>
Here is the motivation.
>
If I have a template like
>
template<class _T, size_t nDim, size_t *p>
class TinyVector
{ };
>
Here nDim is number of dimensions of the array, p saves the size of
the array. This template will have lots of uses for my project.
Lots of uses? Show *one*.
Quote:
such as p = {3, 4, 5}, nDim = 3, _T = double, I defines a 3D Array
with given size 3 * 4 * 5.
If you have a pointer to size_t as a template argument, to instantiate
such template you have to use the address of an object with external
linkage:

extern size_t my_p345[]; // declaration
...
TinyVector<double, 3, my_p345tv;
....
extern size_t my_p345 = { 3,4,5 }; // somewhere -- definition

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


shuisheng
Guest
 
Posts: n/a
#5: Sep 28 '06

re: Problem on non-type template parameter.



Victor Bazarov wrote:
Quote:
shuisheng wrote:
Quote:
Victor Bazarov ??:
Quote:
shuisheng wrote:
>I am wondering how to use the following template
>>
>template<class _T, size_t * _p>
>class A
>{
>};
>
How to use it for what? It seems rather useless.
Here is the motivation.

If I have a template like

template<class _T, size_t nDim, size_t *p>
class TinyVector
{ };

Here nDim is number of dimensions of the array, p saves the size of
the array. This template will have lots of uses for my project.
>
Lots of uses? Show *one*.
For some tensor-like algorithms. Anyway, I did some tests and found
maybe I need

template<class _T, size_t nDim, const size_t *p // <- const here
class TinyArray
{ };

For this new template with const, I tried many ways but still failed.

I appreciate your help!

Shuisheng

I V
Guest
 
Posts: n/a
#6: Sep 28 '06

re: Problem on non-type template parameter.


On Wed, 27 Sep 2006 14:29:42 -0700, shuisheng wrote:
Quote:
If I have a template like
>
template<class _T, size_t nDim, size_t *p>
class TinyVector
{ };
>
Here nDim is number of dimensions of the array, p saves the size of the
array. This template will have lots of uses for my project.
>
such as p = {3, 4, 5}, nDim = 3, _T = double, I defines a 3D Array with
given size 3 * 4 * 5.
I don't have a rigorously worked out reason for this, but using an array to
specify compile-time constants seems wrong to me. Maybe you could try something
like:

#include <iostream>

template<int One = 0, int Two = 0, int Three = 0, int Four = 0, int Five = 0>
class dimensions
{
public:
static const int _1 = One;
static const int _2 = Two;
static const int _3 = Three;
static const int _4 = Four;
static const int _5 = Five;
};

int main()
{
typedef dimensions<3, 4, 5dim;

std::cout << "Dimensions: " << dim::_1 << ", " << dim::_2 << ", " << dim::_3 << std::endl;
}
Closed Thread