Search

Compilation error in case of repeating forward declaration within class by Adam Badura

Closed
as Fixed Help for as Fixed

1
0
Sign in
to vote
Type: Bug
ID: 625710
Opened: 11/30/2010 6:54:41 AM
Access Restriction: Public
1
Workaround(s)
0
User(s) can reproduce this bug
There is bug in the compiler revealed if a forward declaration of a class is reaped within a different class and that declared class is used in some templates (but in a legal way). The issue was discussed at http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/bd2c2ad4-0377-4aea-b312-2b311ede5191.
Details (expand)

Visual Studio/Silverlight/Tooling version

Visual Studio 2010

What category (if any) best represents this feedback?

 

Steps to reproduce

Compile following code:

#include <memory>


template< typename T >
struct wrapper
{
};


class test
{
public:
    static std::shared_ptr< test > create()
    {
        return std::make_shared< test >( wrapper< tag >() );
    }

// Part 1
private:
    struct tag;
public:
    test( const wrapper< tag >& )
    {
    }

// Part 2
private:
    struct tag;
public:
    test( int, const wrapper< tag >& )
    {
    }

};

Product Language

English

Operating System

Windows 7

Operating System Language

Polish

Actual results

Compilation error:
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxshared(13): error C2664: 'test::test(const wrapper<T> &)' : cannot convert parameter 1 from 'wrapper<T>' to 'const wrapper<T> &'
1>     with
1>     [
1>     T=test::tag
1>     ]
1>     Reason: cannot convert from 'wrapper<T>' to 'const wrapper<T>'
1>     with
1>     [
1>     T=test::tag
1>     ]
1>     No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>     c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxshared(35) : see reference to function template instantiation 'std::tr1::_Ref_count_obj<_Ty>::_Ref_count_obj<wrapper<T>>(_Arg0 &&)' being compiled
1>     with
1>     [
1>     _Ty=test,
1>     T=test::tag,
1>     _Arg0=wrapper<test::tag>
1>     ]
1>     c:\users\adam badura\documents\visual studio 2010\projects\test\simple\simple.cpp(15) : see reference to function template instantiation 'std::tr1::shared_ptr<_Ty> std::tr1::make_shared<test,wrapper<T>>(_Arg0 &&)' being compiled
1>     with
1>     [
1>     _Ty=test,
1>     T=test::tag,
1>     _Arg0=wrapper<test::tag>
1>     ]

Expected results

Error-free compilation.
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 2/7/2011 at 10:42 AM
Hi Adam,
    Thanks for reporting the issue.

    A fix for this issue has been checked into the compiler sources. The fix should show up in the next release of Visual C++.

    Note that repeating forward declaration within class is not allowed according to C++ standard, 9.2 Class members:

    A member shall not be declared twice in the member-specification, except that a nested class or member class template can be declared and then later defined.

    VC doesn't fully honor the above requirement, and will accept your code. But it is better not to write code like this.

Xiang Fan
Visual C++ Team
Posted by Microsoft on 11/30/2010 at 7:21 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.
Posted by Adam Badura on 11/30/2010 at 6:56 AM
This particular example can be worked around by moving the test::create function after both Part 1 and Part 2. I don't know if this is also a general workaround for this class of problems.