Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.

Public Report
Report From: C++BuilderX/Compiler/Back end/bcc32/code generation    [ Add a report in this area ]  
Report #:  8735   Status: Need Feedback
Bogus error for static object with private destructor
Project:  C++BuilderX/BC++ 2005 Build #:  1786
Version:    1.0 Submitted By:   Tron Thomas
Report Type:  Basic functionality failure Date Reported:  8/1/2004 3:35:23 PM
Severity:    Serious / Highly visible problem Last Updated: 8/9/2014 5:00:16 PM
Platform:    All versions Internal Tracking #:  
Resolution: Need More Info (Resolution Comments) Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: 1
Description
The Windows compiler for C++ Builder X produces an invalid error when compiling code for a static object that has a private destructor.
Steps to Reproduce:
Compile the following source code:

class Singleton
{
public:
    static Singleton& GetInstance()
    {
        static Singleton singleton;
        return singleton;
    }

private:
    Singleton(const Singleton&);
    Singleton& operator=(const Singleton&);

    Singleton(){}
    ~Singleton(){}
};

int main()
{
    Singleton::GetInstance();

    return 0;
}

Result:
Error E2166 Singleton.cpp 25: Destructor for 'Singleton' is not accessible

Expected:
The code is valid and should compile successfully.  At least two other compiler will compile this code without any errors.
Workarounds
None
Attachment
None
Comments

Tron Thomas at 12/4/2006 4:21:34 PM -
This problems is still exists for version 10.0.2288.42451 of C++ Builder that is included with Turbo C++ Explorer.

This is an obvious and blatant problem.

What is the reason nothing has been done to fix it?

mauro russo at 8/9/2014 12:47:41 PM -
Dear Thomas,

it is not a bug.

If you move the destructor to the public section of your class, and look at the execution steps,

you shall realize that the compiler has to add
a destructor call for the static local object 'singleton'
you declared in the method Singleton::GetInstance().
Such a destruction is invoked after the end of main().

Hence, the destructor has to be accessed from out of
the class.

If you go back to the private section and
remove the call of Singleton::GetInstance(), the error disappears,
because the compiler does not create (and does not have
to destroy) the static object 'singleton', since
no code calls Singleton::GetInstance() anymore.

Mauro.

Server Response from: ETNACODE01