473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem on non-type template parameter.

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

Sep 27 '06 #1
5 1725
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.
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<sianX; // 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<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
Sep 27 '06 #2

Victor Bazarov 写道:
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.

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

Thanks,

Shuisheng

Sep 27 '06 #3
shuisheng wrote:
Victor Bazarov ??:
>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*.
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
Sep 27 '06 #4

Victor Bazarov wrote:
shuisheng wrote:
Victor Bazarov ??:
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

Sep 28 '06 #5
I V
On Wed, 27 Sep 2006 14:29:42 -0700, shuisheng wrote:
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;
}
Sep 28 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
3
by: ThunderMusic | last post by:
Hi, I'm trying to have a MSN Messenger like form/app closing behavior. When I click on the X button, I only want the form to disappear and when I double-click on the notify icon or right-click...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
0
by: Christopher Attard | last post by:
Hi, I need to create a dialog like the 'Add Counters' dialog box in perfmon. I'm using the System.Diagnostics namespace class in .NET and I've managed to do it. The problem arises when I'm...
14
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/...
7
by: Kalpana | last post by:
Hi, As iam working on convertion of C#1.1 to 2.0. Iam facing a problem (screen is flickered) while opening and closing any artifacts. When i tried to debug it is througing ContextSwitchDeadlock...
3
by: Rene | last post by:
Hello to all! For a long time I have been "fighting" a problem compiling an OpenGL program which uses GLUT. First I have put a question in a Watcom group (I want to use this compiler) to which I...
5
by: Mike | last post by:
I use MS SQL EXPRESS DB VS 2005, c# Win Application I have problem "The string is non-numeric" with formula CDbl({pr_DajPonudu;1.KolicinskiPopust})/100 * {@NajamProstora}
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.