BUG: C2512 Instantiating Template in Member Initializer

Last reviewed: March 18, 1998
Article ID: Q171064
The information in this article applies to:
  • The C/C++ Compiler (CL.EXE) included with:

    - Microsoft Visual C++, 32-bit Editions, version 5.0sp1, 5.0sp3

SYMPTOMS

When a member initializer instantiates a template class that takes a const parameter in its constructor, you may receive the following error:

   error C2512: no appropriate default constructor available

RESOLUTION

Declare a global object of the template class specialization that is used in the member initializer. When optimizations are used, this unreferenced global should be optimized away.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

This is a regression from Visual C++ 5.0.

Sample Code

    // Compile options needed: none

    struct _Tree {
      _Tree(const int& _Parg) {}
    };

    template<class _Pr>
    struct set {
      _Tree _Tr;
      set(const _Pr& _Pred = _Pr()) : _Tr(_Pred) {}
    };

    struct Foo
    {
      Foo();
      set<int>& _xSet;
    };

    //set<int> x;  // uncomment for workaround

    Foo::Foo()
     : _xSet(*new set<int>)  //C2512 here
    {}


Additional query words: visual studio service pack
Keywords : VS97BuglistSP3
Version : 5.0sp1,5.0sp3
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 18, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.