By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,693 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

Automagic determination of definition based on definition location.

P: n/a

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
Oct 26 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Jon Slaughter wrote:
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
Oct 26 '05 #2

P: n/a

Jon Slaughter wrote:
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

Oct 26 '05 #3

P: n/a

"mlimber" <ml*****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...

Jon Slaughter wrote:
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


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
Oct 26 '05 #4

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:0_*******************@newsread1.mlpsca01.us.t o.verio.net...
Jon Slaughter wrote:
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.


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
Oct 26 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.