CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    May 2000
    Location
    Germany
    Posts
    369

    explicit template qualifier

    Hello!
    How can i use the following function in the following class?
    Code:
    class test
    {
    public:
          template<class T>
                 T testFunc();
    };
    I tried to invoke the function like this:
    test t;
    1) t.testFunc();
    2) t.testFunc<char>();
    3) t.template testFunc<char>();

    Everytime i tried to invoke the function testFunc in one of the 3 ways i get an error. Why ?
    I am asking this in refer to the class bitset. I tried to use the to_string function and tried to call it in this way:

    bitSet.to_string<std::string::value_type, ....>();

    But it doesn´t work.
    Could someone help me?

    I am using Vc++6.0 and the stlport4.5.3 library.

    Thanks in advance...

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Are you using Service Pack 5 for Visual Studio 6? Try putting the
    function's definition inline. I don't mean putting it later in the
    header file, either; I mean put it right where you first declare the
    function. Visual Studio 6.0 has weird bugs like that sometimes;
    it's not the best when it comes to templates.

    Method #2 seems to be the correct way to call your function.

    --Paul

  3. #3
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    The method 3) is the correct syntax (see TC++PL, 3rd ed.). However, VC++6.0 doesn't support (has a very limited support of) member templates. Sorry.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Originally posted by Gabriel Fleseriu
    The method 3) is the correct syntax (see TC++PL, 3rd ed.). However, VC++6.0 doesn't support (has a very limited support of) member templates. Sorry.
    When you're calling the function, you type:
    Code:
    t.template testFunc<char>();
    Are you sure? If so, what page is this on?

    --Paul

  5. #5
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Originally posted by PaulWendt
    When you're calling the function, you type:
    Code:
    t.template testFunc<char>();
    Are you sure? If so, what page is this on?

    --Paul
    Yes, I'm to 99.9% sure. I don't have the book handy, but I'll come back to this. If you want to look for yourself: Bjarne presents the implementation of an allocator using a MemoryPool class. It's there, somewhere. I remember him commenting on this being a somewhat strange syntax.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  6. #6
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Originally posted by Gabriel Fleseriu
    Yes, I'm to 99.9% sure. I don't have the book handy, but I'll come back to this. If you want to look for yourself: Bjarne presents the implementation of an allocator using a MemoryPool class. It's there, somewhere. I remember him commenting on this being a somewhat strange syntax.
    Wow. I'll go look it up; that doesn't even look like a function call.
    Doesn't #2 work? I've been using that syntax on Visual Studio
    .NET and g++ and it has been working so far.

    --Paul

  7. #7
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Page 858
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  8. #8
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Thanks, Gabriel.
    Now I have to go correct all of this undefined behavior I've been
    relying upon. Stroustrup even cites #2 as an example of a
    syntax error. Whoops

  9. #9
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Hm. Looking at it again, it appears that there is a slight difference
    between his example and what I've been doing the entire time.

    Here's Stroustrup's example [for everyone else to see, if they
    don't posess the book]:

    Code:
    class Memory { // some Allocator
    public:
       template<class T> T* get_new();
       template<class T> void release(T&);
       // ...
    };
    
    template<class Allocator> void f(Allocator& m)
    {
       int* p1 = m.get_new<int>(); // syntax error: int after less-than operator
       int* p2 = m.template get_new<int>(); // explicit qualitification
       // ...
       m.release(p1);
       m.release(p2);
    }
    Now, the difference is that get_new is prototyped to return type
    T*, yet its template parameter is just type T. For clarification
    purposes ... please look at my example.

    Code:
    class Paul
    {
    public:
       template <typename T>
       T convert(const std::string& from);
    };
    Now, to use it:
    Code:
    Paul p;
    int i = p.convert<int>("3435"); // is this undefined? should this be an error?
    I'm just wondering if it has to look like this instead:
    Code:
    Paul p;
    int i = p.template convert<int>("3435");
    Thanks for the enlightenment, Gabriel. I never read the
    appendices in his book ... and now I've got some weekend
    reading, it seems

    --Paul

  10. #10
    Join Date
    May 2000
    Location
    Germany
    Posts
    369
    Thanks a lot to all that tried to help me especially to Paul and Gabriel.
    I tried to compile my code with the gcc 3.2 compiler and the following both versions works.

    2) t.testFunc<char>();
    3) t.template testFunc<char>();

    Thanks alot

  11. #11
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I'm still not sure if the second form there is absolutely
    necessary [the one with the secrewy template thing]. The first
    form even compiled on the Comeau compiler ... so I'm thinking
    that you only need to put .template there when your return
    type doesn't exactly match the type [like if you're returning T*
    instead of T]. I don't know for sure, though.

    --Paul

  12. #12
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    According to the Standard the second version, that is
    Code:
    t.testFunc<char>();
    should produce a compile error because the '<' I marked red should be parsed as the 'less than' operator.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  13. #13
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Thanks, Gabriel. I guess this isn't the first time that three
    compilers have been unanimously wrong about something. I'm
    just happy that I've found out for sure now rather than two years
    down the road when one [or all] of these compilers have
    corrected the behavior.

    --Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured