Thread: question about templates

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    question about templates

    let's say I have a template class:
    Code:
    template<class T>
    class MyTemplate
    {
      public:
        MyTemplate() {  }
        ~MyTemplate() {  }
      private:
        T* t;
    };
    and two other classes, whose constructors take different numbers of parameters:
    Code:
    class A
    {
      public:
        A(int arg1, int arg2) {  }
    };
    
    class B
    {
      public:
        B(int arg1, int arg2, int arg3, int arg4) {  }
    };
    is there a way that I can do the following:
    Code:
    MyTemplate<A>* a = new MyTemplate<A>(1, 2);
    MyTemplate<B>* b = new MyTemplate<B>(1, 2, 3, 4);
    it seems like I've seen this done somewhere before, but I'm not 100% sure.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Your template is the one being ctor'ed not the item you are using to create the template. So long answer is perhaps. The short answer is absolutely not. That is not how templates work.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, this won't work. Template classes are as all other classes - you're trying to create a new instance of MyTemplate class which does not have the constructors you try to use (located in A & B).
    The only thing the type (in your case) does is determine the type of the member pointer t.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    C++0x will have variadic templates and perfect forwarding, which will make this possible.

    Until then, no.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    is there any word yet on when C++0x will be standardized?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    In the meantime, if your template is a container, then build a method for the template to take ownership.

    Other suggestions would really depend on how your template is really going to be used.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    All information about the standardization is available here. In particular, to track the progress of the standard, you'll probably want to read the meeting minutes of the various WG21 meetings.

    Concepts were voted into the draft at the most recent meeting. This has been the biggest outstanding feature addition.
    Also, attributes and user-defined literals were voted in. Those two were also rather controversial.
    Last edited by CornedBee; 10-16-2008 at 03:32 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Show of hands: how many of you have a compiler that perfectly conforms to the current standard?

    Ok, that said. You need to be realistic about C++0x times. Just think of it as sometime within your life time.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by master5001 View Post
    Show of hands: how many of you have a compiler that perfectly conforms to the current standard?
    I don't think there's a compiler that adheres 100%, but there are some compilers that are very close, among them Visual C++ and GCC, I would believe. Or at least they're very good and standards compliant.

    Ok, that said. You need to be realistic about C++0x times. Just think of it as sometime within your life time.
    Given that the current C++ standard is C++98 (I think), I'd say it has taken about, oh... 8 years for Visual C++ to get almost fully compliant with the standard, so count on something like that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It'll go faster. After VC++6 (early 1998) MS did exactly nothing for the compiler for nearly 3 years (VS.Net 2002 came out in very late 2001, I think). Some of the shortcomings of VC++6 were deliberate attempts to sway the standards committee to do things their way, and MS didn't really care about changing it.

    Only when they picked up development again did they improve. The biggest step was VS.Net 2003. The improvements since that have been comparatively small.
    Ironically, the internal version number was 6 for VC++6, 7 for .Net 2002, 8 for .Net 2005 and 9 for .Net 2008, but only 7.1 for .Net 2003.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Microsoft liked to play by their own rules when it came to scoping... amongst other things.

    Microsoft's versioning scheme is screwed. And at the risk of getting flamed I find it hard to make sense of how they decide when something becomes a new version. They release a lot of final products that would be still considered in a beta phase if it were something that I wrote. But that is my two cents, to be honest I am not even going to read posts that argue against me on this particular topic.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by master5001 View Post
    Microsoft's versioning scheme is screwed. And at the risk of getting flamed I find it hard to make sense of how they decide when something becomes a new version. They release a lot of final products that would be still considered in a beta phase if it were something that I wrote. But that is my two cents, to be honest I am not even going to read posts that argue against me on this particular topic.
    You don't need to, either. I fully agree 100% with you there.
    So many things which should never have been released in the first place.
    At least I can hope Windows 7 will be different, since the changes seem to be positive in the development teams (see w7 blog).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    There is more market pressure to stop doing a sloppy job nowadays. I am starting to hear more every day folk asking me how to do _______ on Ubuntu and other flavors of linux. I don't think Microsoft is scared straight... but they have to do some Vista damage control. So from here on out I think a higher degree of quality can be expected.

    And yes... I know that is by far the most pro-microsoft thing I have ever said. I promise it won't happen ever again

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    At least they don't keep things in perpetual beta... not that it would justify microsoft in any way, but that really ticks. Some people can't get thier ........ together in some form of "we're done".

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Hahaha Well normally I don't knock Google, but lately I have been noticing that they do that too. Check this out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. Templates question
    By tezcatlipooca in forum C++ Programming
    Replies: 9
    Last Post: 12-30-2006, 02:08 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Stack Question With Templates
    By Anonymous Freak in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2003, 12:18 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM