|
-
February 24th, 2011, 02:31 PM
#1
Question concerning property functions of templates
Hi together,
my problem is: I have a (managed) template class/structure with property functions like e.g.
template< typename T1 >
public ref struct MyStruct
{
typedef T1 given_type;
...
private:
T1 _element;
public:
property given_type element
{
given_type get() { return _element; }
void set(given_type val) { _element = val; }
}
...
};
It works fine, unless I use a const template parameter like:
MyStruct< const System::Int32 > my;
In this case, the compiler complains about the set function because when T1 is const then the value of _element isn't allowed to change (which is quite right).
Therefore I'm looking for a solution to distinct between const and non-const template parameters. In the case of const parameters I don't want to have the set function.
As a solution I could declare specializations for the whole structure but my real case is much more complicate than the artificial example above and therefore I don't like this solution.
Is there any other solution to solve this problem?
Many thanks in advance,
Physicus.
-
February 24th, 2011, 03:41 PM
#2
Re: Question concerning property functions of templates
I doubt that can be done. I didn't see a way of determining a type's constness from the System::Type object, obtained via T1::typeid in your case. Even if that was possible, I don't see a way to make use of it except using it in an #if which wouldn't work either. (At least I recently tried to use sizeof long in an #if in a native C++ program and that didn't work either.) And even if typeid could be used in prepocessor directives, it would need to be evaluated during template definition (as opposed to instantiation) when T1::typeid wouldn't yet have any meaning anyway. (And evaluatig it during template definition wouldn't gain you anything anyway because it might lead to the template not having an element::set() accessor at all.)
You see, quite a bunch of reasons against what you try to do...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
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
|