-
February 9th, 2015, 05:51 AM
#1
template class with template members
I have a class like this
PHP Code:
template<class X>
class A
{
X m_x;
public:
X* foo();
X* bar();
//others are not related to X
};
I would like to get rid of
PHP Code:
template<class X>
for class level but still use it for members. Like this
PHP Code:
class A
{
X m_x;
public:
template<class X>
X* foo();
template<class X>
X* bar();
//others are not related to X
};
However, I am still stuck at
-
February 9th, 2015, 07:08 AM
#2
Re: template class with template members
You cannot do that since m_x is a member variable, so its type must be available at that point.
-
February 11th, 2015, 03:45 AM
#3
Re: template class with template members
You can use void* to get rid of the outer template
Code:
class A
{
void* m_x;
public:
void* foo();
void* bar();
//others are not related to X
};
-
February 11th, 2015, 04:07 AM
#4
Re: template class with template members
Yuk!!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 11th, 2015, 08:22 AM
#5
Re: template class with template members
if you make X a member of A then the X class needs to be fully defined by the time the compiler tries to compile the A class.
if you make a pointer to X or a reference to X member, then you only need a forward reference to X, and the actual definition of X can come later.
you don't need a template function to return an X*, a forward is enough. You will probably need the full X definition when implementing said function though.
-
February 13th, 2015, 08:26 AM
#6
Re: template class with template members
Originally Posted by BenchMark
You can use void* to get rid of the outer template
Code:
class A
{
void* m_x;
public:
void* foo();
void* bar();
//others are not related to X
};
That's not really a solution. How will this code be able to create/copy/destroy an instance of X?
@Tiny Dolls
Why do you want to get rid of the class template? What do you want to accomplish at a higher level?
If I had to guess, I'd guess you want to implement some kind of type erasure.
...but I don't like guessing, so I won't elaborate unless I know it's worth the effort.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
February 14th, 2015, 01:23 PM
#7
Re: template class with template members
Originally Posted by D_Drmmr
That's not really a solution. How will this code be able to create/copy/destroy an instance of X?
@Tiny Dolls
Why do you want to get rid of the class template? What do you want to accomplish at a higher level?
If I had to guess, I'd guess you want to implement some kind of type erasure.
...but I don't like guessing, so I won't elaborate unless I know it's worth the effort.
Yes I think this is what I am looking for.
I also tried what Benchmark suggested and it compilable. But as you mentioned I am wondering how coding like this would result in anything bad
PHP Code:
void* pv=(list<string>)GetIPList();
//something very long processed and processing
list<string> ret=pv;
return ret;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|