Namespace/Template member initialization issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Ross

    Namespace/Template member initialization issue

    Hi,

    I am having a problem writing a constructor/member initialization with
    VC.NET7.1.

    Here is the code:

    ---

    namespace Library
    {
    template <typename T>
    class LibraryTemplate T
    { };
    }

    namespace Module
    {
    class ModuleObject : public Library::Librar yTemplateT<int>
    {
    public:
    ModuleObject() throw();
    };

    // [1] FAILS: error C2614: 'Module::Module Object' : illegal member
    initialization: 'LibraryTemplat eT' is not a base or member
    // ModuleObject::M oduleObject() throw() : LibraryTemplate T()
    // { }

    // [2] FAILS: error C2059: syntax error : '<'
    // ModuleObject::M oduleObject() throw() : LibraryTemplate T<int>()
    // { }

    // [3] WORKS
    ModuleObject::M oduleObject() throw() : Library::Librar yTemplateT<int> ()
    { }

    }
    ---

    The weird thing is that if LibraryTemplate T is a class and not a template
    then I am able to use the first option above and not have to manually
    specify the Library namespace. Also, all three choices work when trying them
    online at http://www.comeaucomputing.com/tryitout/.

    Since I have existing code that is using option [1] above for non-templates
    I would like to know:

    - Is the code illegal (for both templates and regular classes). ie. is
    option [3] the only legal way to write that code (without having a using
    declaration)?
    - Are templates somehow special and get resolved differently?
    - Is this a bug?

    Thanks


  • John Harrison

    #2
    Re: Namespace/Template member initialization issue


    "Brian Ross" <brian.ross.x@r ogers.com> wrote in message
    news:ZHL%a.2127 59$rsJ.76040@ne ws04.bloor.is.n et.cable.rogers .com...[color=blue]
    > Hi,
    >
    > I am having a problem writing a constructor/member initialization with
    > VC.NET7.1.
    >
    > Here is the code:
    >
    > ---
    >
    > namespace Library
    > {
    > template <typename T>
    > class LibraryTemplate T
    > { };
    > }
    >
    > namespace Module
    > {
    > class ModuleObject : public Library::Librar yTemplateT<int>
    > {
    > public:
    > ModuleObject() throw();
    > };
    >
    > // [1] FAILS: error C2614: 'Module::Module Object' : illegal member
    > initialization: 'LibraryTemplat eT' is not a base or member
    > // ModuleObject::M oduleObject() throw() : LibraryTemplate T()
    > // { }
    >
    > // [2] FAILS: error C2059: syntax error : '<'
    > // ModuleObject::M oduleObject() throw() : LibraryTemplate T<int>()
    > // { }
    >
    > // [3] WORKS
    > ModuleObject::M oduleObject() throw() :[/color]
    Library::Librar yTemplateT<int> ()[color=blue]
    > { }
    >
    > }
    > ---
    >
    > The weird thing is that if LibraryTemplate T is a class and not a template
    > then I am able to use the first option above and not have to manually
    > specify the Library namespace. Also, all three choices work when trying[/color]
    them[color=blue]
    > online at http://www.comeaucomputing.com/tryitout/.
    >
    > Since I have existing code that is using option [1] above for[/color]
    non-templates[color=blue]
    > I would like to know:
    >
    > - Is the code illegal (for both templates and regular classes). ie. is
    > option [3] the only legal way to write that code (without having a using
    > declaration)?
    > - Are templates somehow special and get resolved differently?
    > - Is this a bug?
    >
    > Thanks
    >[/color]

    The following code compiles for me using VC++ 7.1

    namespace Library
    {
    template <typename T>
    class LibraryTemplate T
    { };
    }
    namespace Module
    {
    class ModuleObject : public Library::Librar yTemplateT<int>
    {
    public:
    ModuleObject() throw() {}
    };
    }
    int main()
    {
    Module::ModuleO bject m;
    }

    Suggest you post the exact code you are having trouble with.

    john


    Comment

    • Brian Ross

      #3
      Re: Namespace/Template member initialization issue


      "John Harrison" <john_andronicu s@hotmail.com> wrote in message
      news:bho2j5$1cq fp$1@ID-196037.news.uni-berlin.de...[color=blue]
      >
      > "Brian Ross" <brian.ross.x@r ogers.com> wrote in message
      > news:ZHL%a.2127 59$rsJ.76040@ne ws04.bloor.is.n et.cable.rogers .com...[color=green]
      > > Hi,
      > >
      > > I am having a problem writing a constructor/member initialization with
      > > VC.NET7.1.
      > >
      > > Here is the code:
      > >
      > > ---
      > >
      > > namespace Library
      > > {
      > > template <typename T>
      > > class LibraryTemplate T
      > > { };
      > > }
      > >
      > > namespace Module
      > > {
      > > class ModuleObject : public Library::Librar yTemplateT<int>
      > > {
      > > public:
      > > ModuleObject() throw();
      > > };
      > >
      > > // [1] FAILS: error C2614: 'Module::Module Object' : illegal member
      > > initialization: 'LibraryTemplat eT' is not a base or member
      > > // ModuleObject::M oduleObject() throw() : LibraryTemplate T()
      > > // { }
      > >
      > > // [2] FAILS: error C2059: syntax error : '<'
      > > // ModuleObject::M oduleObject() throw() : LibraryTemplate T<int>()
      > > // { }
      > >
      > > // [3] WORKS
      > > ModuleObject::M oduleObject() throw() :[/color]
      > Library::Librar yTemplateT<int> ()[color=green]
      > > { }
      > >
      > > }
      > > ---
      > >
      > > The weird thing is that if LibraryTemplate T is a class and not a[/color][/color]
      template[color=blue][color=green]
      > > then I am able to use the first option above and not have to manually
      > > specify the Library namespace. Also, all three choices work when trying[/color]
      > them[color=green]
      > > online at http://www.comeaucomputing.com/tryitout/.
      > >
      > > Since I have existing code that is using option [1] above for[/color]
      > non-templates[color=green]
      > > I would like to know:
      > >
      > > - Is the code illegal (for both templates and regular classes). ie. is
      > > option [3] the only legal way to write that code (without having a using
      > > declaration)?
      > > - Are templates somehow special and get resolved differently?
      > > - Is this a bug?
      > >
      > > Thanks
      > >[/color]
      >
      > The following code compiles for me using VC++ 7.1
      >
      > namespace Library
      > {
      > template <typename T>
      > class LibraryTemplate T
      > { };
      > }
      > namespace Module
      > {
      > class ModuleObject : public Library::Librar yTemplateT<int>
      > {
      > public:
      > ModuleObject() throw() {}
      > };
      > }
      > int main()
      > {
      > Module::ModuleO bject m;
      > }
      >
      > Suggest you post the exact code you are having trouble with.[/color]

      Hi,

      I think you missed the part where I mentioned that I have tried your
      combination and I know that it works (look at the comments in my sample
      code). My question was more about why the other variations work when not
      using templates and if that code is illegal.

      Brian


      Comment

      • John Harrison

        #4
        Re: Namespace/Template member initialization issue

        >[color=blue]
        > Hi,
        >
        > I think you missed the part where I mentioned that I have tried your
        > combination and I know that it works (look at the comments in my sample
        > code). My question was more about why the other variations work when not
        > using templates and if that code is illegal.
        >
        > Brian
        >[/color]

        You're right I missed the point, sorry about that. I surprised that 1 and 2
        work at all even for non-templates, so I don't think I should say any more.

        john


        Comment

        Working...