templated return

R

RA Scheltema

hi all,


I have the following situation:

template <typename _Ta>
class A
{
template <size_t _Tsize>
struct B
{
char buffer[_Tsize];
};
};


The thing is, I need to define a function which returns struct B, but not
with a pre-defined template-argument _Tsize. I have been trying some things
like:

template <typename _Ta>
class A
{
...
template <typename _Tsize>
B<_Tsize> GetB()
{
return B<_Tsize>();
}
};


and attempted to call this function as follows:

A<int> a;
A<int>::B<1> b = a.GetB<1>();


But this doesn't seem to make it through my Visual C++ 6.0 compiler (wierd
stuff with operators for the std::vector are popping up). Can anybody tell
me if there even is a standard c++ way for doing this ?



thanks ahead,
richard
 
R

red floyd

RA said:
hi all,


I have the following situation:

template <typename _Ta>
class A
{
template <size_t _Tsize>
struct B
{
char buffer[_Tsize];
};
};


The thing is, I need to define a function which returns struct B, but not
with a pre-defined template-argument _Tsize. I have been trying some things
like:

template <typename _Ta>
class A
{
...
template <typename _Tsize>
B<_Tsize> GetB()
{
return B<_Tsize>();
}
};


and attempted to call this function as follows:

A<int> a;
A<int>::B<1> b = a.GetB<1>();


But this doesn't seem to make it through my Visual C++ 6.0 compiler (wierd
stuff with operators for the std::vector are popping up). Can anybody tell
me if there even is a standard c++ way for doing this ?



thanks ahead,
richard

Rename your template parameters. Leading underscore followed by a cap is reserved.
 
V

Victor Bazarov

RA Scheltema said:
I have the following situation:

template <typename _Ta>

If this is your code, you shouldn't be using such names. Any name
that starts with an underscore and a capital letter is _reserved_.
Why in the world do you need the leading underscore?
class A
{
template <size_t _Tsize>

Same here.
struct B
{
char buffer[_Tsize];
};
};


The thing is, I need to define a function which returns struct B, but not
with a pre-defined template-argument _Tsize. I have been trying some things
like:

template <typename _Ta>
class A
{
...
template <typename _Tsize>
B<_Tsize> GetB()
{
return B<_Tsize>();
}
};


and attempted to call this function as follows:

A<int> a;
A<int>::B<1> b = a.GetB<1>();


But this doesn't seem to make it through my Visual C++ 6.0 compiler (wierd
stuff with operators for the std::vector are popping up). Can anybody tell
me if there even is a standard c++ way for doing this ?

Try a different compiler. VC++ 6 is really bad wrt templates.

Victor
 
A

Andrey Tarasevich

RA said:
...
I have the following situation:

template <typename _Ta>
class A
{
template <size_t _Tsize>
struct B
{
char buffer[_Tsize];
};
};


The thing is, I need to define a function which returns struct B, but not
with a pre-defined template-argument _Tsize. I have been trying some things
like:

template <typename _Ta>
class A
{
...
template <typename _Tsize>
B<_Tsize> GetB()
{
return B<_Tsize>();
}
};
...

This doesn't make much sense. Template 'B' is declared as accepting a
non-type parameter (a value of type 'size_t') and you are trying to
specialize it with type argument.

Here again, you are trying to specialize 'GetB' with non-type argument,
while template 'GetB' expects a type argument.
 

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,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top