Problem on non-type template parameter.

S

shuisheng

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<si> anX; // C2970 cannot use static variable in templates

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


Thanks,

Shuisheng
 
V

Victor Bazarov

shuisheng said:
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.
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.
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<si> anX; // C2970 cannot use static variable in templates

A non-template integral argument requires compile-time constant.
// this would also work
const int i = 10;
X<i> anX2;

Yes, here it's fine.

V
 
S

shuisheng

Victor Bazarov 写é“:
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
 
V

Victor Bazarov

shuisheng said:
Victor Bazarov ??:


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*.
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_p345> tv;
....
extern size_t my_p345 = { 3,4,5 }; // somewhere -- definition

V
 
S

shuisheng

Victor said:
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

I V

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, 5> dim;

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,002
Messages
2,570,261
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top