Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Template type as member variable?

57 views
Skip to first unread message

Heinz-Mario Frühbeis

unread,
May 3, 2016, 2:57:20 AM5/3/16
to
Hi,

is it possible to store a type in a member variable and to use it again
for a template function?

class c_Area{
public:
template <typename t>
t* Area_T(t *vT){
static t *nT;
if(vT == NULL){
return nT;
} else {
nT = vT;
return nT;
}
}

std::string Name;
std::string Type;
};


Init:
nArea.Area_T < t > (nT);

Ask:
c_Area nArea;
cout << nArea.Area_T < ??? > (NULL)->Name();

'???' -> sure I know, what type is stored in Area_T, but is it maybe
possible to store the type of Area_T in c_Area so that I can use it
again for e.g. 'Ask'?

E.g.:
c_Area nArea;
cout << nArea.Area_T < nArea.TemplateType > (NULL)->Name();

Instead of:
c_Area nArea;
if(nArea.Type == "that_type")
cout << nArea.Area_T < that_type > (NULL)->Name();

Regards
Heinz-Mario Frühbeis

Öö Tiib

unread,
May 3, 2016, 5:54:27 AM5/3/16
to
On Tuesday, 3 May 2016 09:57:20 UTC+3, Heinz-Mario Frühbeis wrote:
> Hi,
>
> is it possible to store a type in a member variable and to use it again
> for a template function?

All types given as template argument must be compile-time known to
compiler in C++. For rum-time polymorphism there are base classes
and virtual member functions.

Richard

unread,
May 3, 2016, 6:22:42 PM5/3/16
to
[Please do not mail me a copy of your followup]

=?UTF-8?Q?Heinz-Mario_Fr=c3=bchbeis?= <D...@Earlybite.individcore.de> spake the secret code
<ng9i45$vjn$1...@news.albasani.net> thusly:

>is it possible to store a type in a member variable and to use it again
>for a template function?

Can you explain more the use case of why you would need to do this?

More generally, please describe the goal you are trying to achieve and
why you think this specific task is the way to achieve it.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Heinz-Mario Frühbeis

unread,
May 5, 2016, 1:42:46 AM5/5/16
to
Ok, thank you for the information.

Regards
Heinz-Mario Frühbeis

Heinz-Mario Frühbeis

unread,
May 5, 2016, 1:44:13 AM5/5/16
to
Am 03.05.2016 um 15:29 schrieb Stefan Ram:
> =?UTF-8?Q?Heinz-Mario_Fr=c3=bchbeis?= <D...@Earlybite.individcore.de> writes:
>> is it possible to store a type in a member variable and to use it again
>> for a template function?
>
> No, but one can write the following.
>
> #include <iostream>
> #include <ostream>
> #include <typeinfo>
>
> struct clazz { using membervariable = int; };
>
> template< typename T >void f()
> { ::std::cout << typeid( T ).name() << '\n'; }
>
> int main() { f< clazz::membervariable >(); }
>

Currently it "says" nothing to me, but as soon as I find some time I
will a look on it and I will answer again.

Thanks, sir.
Regards
Heinz-Mario Frühbeis

Heinz-Mario Frühbeis

unread,
May 5, 2016, 1:49:52 AM5/5/16
to
Am 04.05.2016 um 00:22 schrieb Richard:
> [Please do not mail me a copy of your followup]
>
> =?UTF-8?Q?Heinz-Mario_Fr=c3=bchbeis?= <D...@Earlybite.individcore.de> spake the secret code
> <ng9i45$vjn$1...@news.albasani.net> thusly:
>
>> is it possible to store a type in a member variable and to use it again
>> for a template function?
>
> Can you explain more the use case of why you would need to do this?
>
> More generally, please describe the goal you are trying to achieve and
> why you think this specific task is the way to achieve it.
>

Isn'T it self-declaring?
It would simplify coding, nothing more. It would result in less code...
Currently I use void* to store a member in a vector and I, if used, cast
this void-pointer into a template-function. This works, (IMHO) less
coding...
I hope it has reached your interest...
Til then
Heinz-Mario Frühbeis

Jorgen Grahn

unread,
May 6, 2016, 2:23:40 AM5/6/16
to
On Thu, 2016-05-05, Heinz-Mario Frühbeis wrote:
> Am 04.05.2016 um 00:22 schrieb Richard:
>> [Please do not mail me a copy of your followup]
>>
>> =?UTF-8?Q?Heinz-Mario_Fr=c3=bchbeis?= <D...@Earlybite.individcore.de> spake the secret code
>> <ng9i45$vjn$1...@news.albasani.net> thusly:
>>
>>> is it possible to store a type in a member variable and to use it again
>>> for a template function?
>>
>> Can you explain more the use case of why you would need to do this?
>>
>> More generally, please describe the goal you are trying to achieve and
>> why you think this specific task is the way to achieve it.
>>
>
> Isn'T it self-declaring?

No. He asked for a use case.

And I suspect he asked because

a) He didn't really understand what you want (I didn't either).

b) If you show your real-life problem, people will be familiar with it
and tell you how they deal with it. It's not unlikely that someone
has a different, better solution.

Do you perhaps have a Python background? People here have experience
mapping Python solutions to C++ solutions, too.

> It would simplify coding, nothing more. It would result in less code...

> Currently I use void* to store a member in a vector and I, if used, cast
> this void-pointer into a template-function. This works, (IMHO) less
> coding...

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
0 new messages