Template parameter conflicts with typedef in template argument 
Author Message
 Template parameter conflicts with typedef in template argument

I've encountered something that looks to me as either a bug in MSVC6
SP5, or a C++ standard defect assuming the compiler is conforming in
this regard (which I strongly doubt). Consider this code:

template <class Base>
struct Wrapper : public Base
{
    void f(Base *p) {}

Quote:
};

struct Break
{
    typedef int Base;

Quote:
};

int main()
{
    Break b;
    Wrapper<Break> w;
    w.f(&b);
    return 0;

Quote:
}

MSVC6 produces the following error:

C:\Projects\Test\Test.cpp(21) : error C2664: 'f' : cannot convert
parameter 1 from 'struct Break *' to 'int *'
        Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast

Clearly, when instantiating Wrapper<Break>, Base in Wrapper is taken to
mean Break::Base rather than Break. The same problem happens if Break
defines a data or function member named Base, or a enum with constant
named Base - in short, whenever Break introduces name Base at class
scope.

This means that many templates may break if the class they are
instantiated with happens to define a name that conflicts with the name
of the template argument. I don't believe it is conforming to the
standard, and indeed Comeau Online compiler has no problem with this
snippet.

Can anyone check if this problem still exists in VC7?
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Tue, 13 Jul 2004 00:41:49 GMT  
 Template parameter conflicts with typedef in template argument

Quote:
>MSVC6 produces the following error:

>C:\Projects\Test\Test.cpp(21) : error C2664: 'f' : cannot convert
>parameter 1 from 'struct Break *' to 'int *'
>        Types pointed to are unrelated; conversion requires
>reinterpret_cast, C-style cast or function-style cast
>Can anyone check if this problem still exists in VC7?

VC7 (RC4) produces:

c:\atest\atest.cpp(21): error C2664: 'Wrapper<struct Break>::f' :
cannot convert parameter 1 from 'Break *__w64 ' to 'Break::Base *'

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Tue, 13 Jul 2004 01:31:57 GMT  
 Template parameter conflicts with typedef in template argument
Confirmed by MS to be a bug in both VC6 and VC7. No estimate on the fix
availability.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> I've encountered something that looks to me as either a bug in MSVC6
> SP5, or a C++ standard defect assuming the compiler is conforming in
> this regard (which I strongly doubt). Consider this code:

> template <class Base>
> struct Wrapper : public Base
> {
>     void f(Base *p) {}
> };

> struct Break
> {
>     typedef int Base;
> };

> int main()
> {
>     Break b;
>     Wrapper<Break> w;
>     w.f(&b);
>     return 0;
> }

> MSVC6 produces the following error:

> C:\Projects\Test\Test.cpp(21) : error C2664: 'f' : cannot convert
> parameter 1 from 'struct Break *' to 'int *'
>         Types pointed to are unrelated; conversion requires
> reinterpret_cast, C-style cast or function-style cast

> Clearly, when instantiating Wrapper<Break>, Base in Wrapper is taken
to
> mean Break::Base rather than Break. The same problem happens if Break
> defines a data or function member named Base, or a enum with constant
> named Base - in short, whenever Break introduces name Base at class
> scope.

> This means that many templates may break if the class they are
> instantiated with happens to define a name that conflicts with the
name
> of the template argument. I don't believe it is conforming to the
> standard, and indeed Comeau Online compiler has no problem with this
> snippet.

> Can anyone check if this problem still exists in VC7?
> --
> With best wishes,
>     Igor Tandetnik

> "For every complex problem, there is a solution that is simple, neat,
> and wrong." H.L. Mencken



Sun, 18 Jul 2004 01:06:15 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. VC7 does NOT support template-template parameters involving non-type parameters

2. typedef with template arguments...

3. template arguments used within a template function - how?

4. explicit template arguments to function templates

5. Template argument dependant default parameter

6. VC 7.1 final beta: bug with template template parameters

7. template template parameter with default value (VS.NET 2003)

8. Possible bug with template template parameters in VC++ 7.0

9. Bug in inheritance from template template parameters in VC7

10. Templates as a templates-parameters

11. Template class as template parameter

12. template template parameters

 

 
Powered by phpBB® Forum Software