Automagic determination of definition based on definition location.

J

Jon Slaughter

At the bottom of the page on

http://ootips.org/yonat/4dev/smart-pointers.html

It is noted that one would use different types of pointers based on
different "locations" in the code. What I'm wondering is if there is a way
to have the compiler automatically determine which type to use depending on
its location such as

class T
{
MyUberPointer<int> i;
}

vs.

void func()
{
myUberPointer<int> i;
}


so that MyUberPointer uses a copied pointer while myUberPointer uses an auto
pointer

one might have some policy based parameter so that one could override the
default, but in this case the default is "location" dependent. i.e., it
would be nice if there was an implicit parameter passed to templates that
held the location of its decleration similar to the way the this pointer
works... then one could have seperate template definitions depending on the
location. Hence one could easily have "polymorphism" of types based on
"location". I do know one can easily do this by having some extra parameter
such as


class T
{
UberPointer<int,1> i;
}

vs.

void func()
{
UberPointer<int,0> i;
}

but ofcourse this isn't the best way.


Jon
 
V

Victor Bazarov

Jon said:
At the bottom of the page on

http://ootips.org/yonat/4dev/smart-pointers.html

It is noted that one would use different types of pointers based on
different "locations" in the code. What I'm wondering is if there is a way
to have the compiler automatically determine which type to use depending on
its location such as

class T
{
MyUberPointer<int> i;
}

vs.

void func()
{
myUberPointer<int> i;
}


so that MyUberPointer uses a copied pointer while myUberPointer uses an auto
pointer

one might have some policy based parameter so that one could override the
default, but in this case the default is "location" dependent. i.e., it
would be nice if there was an implicit parameter passed to templates that
held the location of its decleration similar to the way the this pointer
works... then one could have seperate template definitions depending on the
location. Hence one could easily have "polymorphism" of types based on
"location". I do know one can easily do this by having some extra parameter
such as


class T
{
UberPointer<int,1> i;
}

vs.

void func()
{
UberPointer<int,0> i;
}

but ofcourse this isn't the best way.

Why isn't it? Imagine that you want to use an auto pointer inside
a class or a copied pointer inside a function. What then? Suddenly the
system (or the compiler) is choosing for you and you seem to have no
control over the result. Develop a policy template/class and use it.
First, it's explicit, so you have full control. Second, it's self-
documenting:

void func()
{
UberPointer<int,AutoPointer> i;
}

class T
{
UberPointer<int,CopyPointer> i;
};

More on policy-based programming in "Modern C++ Design" and elsewhere.

V
 
M

mlimber

Jon said:
At the bottom of the page on

http://ootips.org/yonat/4dev/smart-pointers.html

It is noted that one would use different types of pointers based on
different "locations" in the code. What I'm wondering is if there is a way
to have the compiler automatically determine which type to use depending on
its location such as

class T
{
MyUberPointer<int> i;
}

vs.

void func()
{
myUberPointer<int> i;
}


so that MyUberPointer uses a copied pointer while myUberPointer uses an auto
pointer

one might have some policy based parameter so that one could override the
default, but in this case the default is "location" dependent. i.e., it
would be nice if there was an implicit parameter passed to templates that
held the location of its decleration similar to the way the this pointer
works... then one could have seperate template definitions depending on the
location. Hence one could easily have "polymorphism" of types based on
"location". I do know one can easily do this by having some extra parameter
such as


class T
{
UberPointer<int,1> i;
}

vs.

void func()
{
UberPointer<int,0> i;
}

but ofcourse this isn't the best way.


Jon

There's no way to determine the context at compile time that I know of,
and there probably shouldn't be. IMHO, the latter code *is* actually
the best way (though not with 1 and 0; meaningful policy names would be
better) because having the same code behave differently in different
contexts could easily lead to implicit (read: hard-to-find) bugs.

Cheers! --M
 
J

Jon Slaughter

mlimber said:
There's no way to determine the context at compile time that I know of,
and there probably shouldn't be. IMHO, the latter code *is* actually
the best way (though not with 1 and 0; meaningful policy names would be
better) because having the same code behave differently in different
contexts could easily lead to implicit (read: hard-to-find) bugs.

Cheers! --M

Yeah, maybe your right ;) I guess ultimately it could cause more problems
than it would solve and theres probably no case where you really need it.

Jon
 
J

Jon Slaughter

Victor Bazarov said:
Why isn't it? Imagine that you want to use an auto pointer inside
a class or a copied pointer inside a function. What then? Suddenly the
system (or the compiler) is choosing for you and you seem to have no
control over the result.

Ofcourse I never said one couldn't override the default.
Develop a policy template/class and use it.
First, it's explicit, so you have full control. Second, it's self-
documenting:

void func()
{
UberPointer<int,AutoPointer> i;
}

class T
{
UberPointer<int,CopyPointer> i;
};


Yeah, I guess its probably the better way simply because it makes it
explicit and I suppose there is no case that one could need seperate code
for a class and for a function(local variable). Though I can't see it
causing major problems if the programmer really knows what he is doing but
ofcourse thats a pipe dream ;)

(I did think of the policy based design and thought I mentioned it but I
guess I didn't).
More on policy-based programming in "Modern C++ Design" and elsewhere.

V

Thanks,
Jon
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top