Automagic determination of definition based on definition location.

Discussion in 'C++' started by Jon Slaughter, Oct 26, 2005.

  1. 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
    Jon Slaughter, Oct 26, 2005
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Oct 26, 2005
    #2
    1. Advertising

  3. Jon Slaughter

    mlimber Guest

    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
    mlimber, Oct 26, 2005
    #3
  4. "mlimber" <> wrote in message
    news:...
    >
    > 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
    Jon Slaughter, Oct 26, 2005
    #4
  5. "Victor Bazarov" <> wrote in message
    news:0_N7f.43290$01.us.to.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
    Jon Slaughter, Oct 26, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aleks Kissinger

    Automagic class prototyping

    Aleks Kissinger, Jul 25, 2006, in forum: Ruby
    Replies:
    6
    Views:
    137
    Logan Capaldo
    Jul 26, 2006
  2. Mauricio Fernandez
    Replies:
    1
    Views:
    143
    Peña, Botp
    Oct 16, 2006
  3. Mauricio Fernandez
    Replies:
    4
    Views:
    109
    Mauricio Fernandez
    Dec 30, 2006
  4. Mauricio Fernandez
    Replies:
    2
    Views:
    88
    Benjamin Ritcey
    Jan 14, 2007
  5. Mauricio Fernandez
    Replies:
    4
    Views:
    128
    Mauricio Fernandez
    Feb 8, 2007
Loading...

Share This Page