Discussion:
Problems with templates in Turbo C++
(too old to reply)
Darko Miletic
2008-07-10 14:47:03 UTC
Permalink
Here is my code (this is not the real code but represents my problem
exactly):


#ifdef __BORLANDC__
#pragma warn -inl
#endif __BORLANDC__

struct policyA {
static void doinit() {}
static void dofree() {}
};

template < typename T >
struct policyB {
static void doinit() { T::instance().get(); }
static void dofree() {}
};

template < typename HandleT, typename PolicyT >
struct singleton {
static singleton& instance (void) {
static singleton inst;
return inst;
};
HandleT get(void) const { return handle_;}
private: //commenting this makes it compile on bds
~singleton(void) {
PolicyT::dofree();
}
private:
singleton(void) : handle_(0) {
PolicyT::doinit();
}
singleton(singleton const&);
singleton& operator=(singleton const&);
volatile HandleT handle_;
};


typedef singleton< void*, policyA > singletonA;
typedef singleton< void*, policyB< singletonA > > singletonB;


int main(void) {

singletonB::instance().get();

return 0;
}


If I compile it with MSVC or comeau everything is fine, however on BDS
2006 it says:

bcc32 -q -tWC -w -Q+ prbl.cpp
prbl.cpp:
Error E2166 prbl.cpp 49: Destructor for 'singletonA' is not accessible
*** 1 errors in Compile ***

Only by commenting private specification (making destructor public) it
compiles fine.

Now who is right here? msvc/comeau or bds?
Alan Bellingham
2008-07-10 15:37:47 UTC
Permalink
Post by Darko Miletic
Here is my code (this is not the real code but represents my problem
A simplification - without the templates, but with the same error
message, at least under BCB5.

struct singleton
{
static singleton& instance()
{
static singleton inst;
return inst;
};

private: //commenting this makes it compile on bds
~singleton(void);
};

int main(void)
{
singleton::instance();
}

// Reported error line at end of file ...

It's an access issue, and not a template one - for some reason, the
destructor doesn't appear to be accessible within a member function of
the same class. The compiler seems to be saying to itself "OK, this
object is static, and therefore needs to be destroyed at the end of the
program run. The point at which the d'tor is actually run isn't within
the function, it's at global scope.

Here's a restructuring that shows exactly the same problem:

struct singleton
{
static singleton& instance()
{
return s_singleton;
};

private: //commenting this makes it compile on bds
~singleton(void);

static singleton s_singleton;
};

singleton singleton::s_singleton;

int main(void)
{
singleton::instance();
}

Again, the access error. But here, the singleton object is outside the
method, even though it's declared within the class. Yet Comeau compiles
it happily.

Frankly, I think BCB/BDS is at fault here. IMO, the access requirements
should be as per the declaration point, not wherever the compiler might
actually be generating the call. But please don't ask me to track down
the relevant part of the standard.

Alan Bellingham
--
Team Browns
ACCU Conference 2009: to be announced
Thomas Maeder [TeamB]
2008-07-10 15:45:43 UTC
Permalink
Post by Darko Miletic
bcc32 -q -tWC -w -Q+ prbl.cpp
Error E2166 prbl.cpp 49: Destructor for 'singletonA' is not accessible
*** 1 errors in Compile ***
Only by commenting private specification (making destructor public)
it compiles fine.
Now who is right here? msvc/comeau or bds?
MSVC, Comeau (and gcc).

The destructor *is* accessible where it has to be (in the instance()
member function).

Cf. http://qc.codegear.com/wc/qcmain.aspx?d=7391

Loading...