Namespace/Template member initialization issue

  • Thread starter Thread starter Brian Ross
  • Start date Start date
B

Brian Ross

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 LibraryTemplateT
{ };
}

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

// [1] FAILS: error C2614: 'Module::ModuleObject' : illegal member
initialization: 'LibraryTemplateT' is not a base or member
// ModuleObject::ModuleObject() throw() : LibraryTemplateT()
// { }

// [2] FAILS: error C2059: syntax error : '<'
// ModuleObject::ModuleObject() throw() : LibraryTemplateT<int>()
// { }

// [3] WORKS
ModuleObject::ModuleObject() throw() : Library::LibraryTemplateT<int>()
{ }

}
---

The weird thing is that if LibraryTemplateT 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
 
Brian,
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?
I think Comeau is correct. The name of the class template is injected.
In explicit specializations the injected-class-name without the
argument
list refers to the specialization not to the template. So all three
forms
should be correct IMHO.

-hg
 
- 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)?

VC++ is correct about [1] but incorrect about [2], ie it's legal and should
work.
- Are templates somehow special and get resolved differently?

Name lookup is much more complicated for templates. [1] fails because a
locally declared name is only valid within the scope of the class template
that it refers to. If you want to refer to LibraryTemplateT, within the
scope of ModuleObject, then you must use a template-id (the template-name
followed by <template arg.> for instance LibraryTemplateT<int>). [2] should
work because when declaring a class, the class-name is inserted in the class
itself.
- Is this a bug?

Yes, [2] is a bug.

Bo-Staffan
 
Back
Top