Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem /w constructor in template

Reply
Thread Tools

Problem /w constructor in template

 
 
dlidstrom@gmail.com
Guest
Posts: n/a
 
      10-12-2006
Hello!

The following code compiles with msvc-7.1, but gcc-3.4.4 fails. I fail
too, in seeing why.


#include <iostream>
#include <string>
#include <stdexcept> // logic_error

struct IgnoreErrorCode
{};

struct MandatoryErrorCodeException : public std::logic_error
{
MandatoryErrorCodeException(const char* s)
: std::logic_error(s)
{ }
};

template<class CODE> class ErrorCode;

template<class CODE>
class ThrowableErrorCode
{
friend class ErrorCode<CODE>;
CODE m_code;
bool m_throw;
public:
//! constructor - receive the error code and arm the exception
ThrowableErrorCode(CODE i_code)
: m_code(i_code),
m_throw(true)
{}

//! explicitly ignore the error code and avoid exception
operator IgnoreErrorCode()
{
m_throw = false;
return IgnoreErrorCode();
}
~ThrowableErrorCode()
{
//! will throw unless ErrorCode<CODE> or IgnoreError will prevent
it
if( m_throw )
throw MandatoryErrorCodeException("Must handle error code");
}
};

template<class T>
class ErrorCode
{
T m_code;

public:

ErrorCode(ThrowableErrorCode<T>& code)
: m_code(code.m_code)
{
// prevent the throw
code.m_throw = false;
}
operator T()
{
return this->m_code;
}
};

ThrowableErrorCode<bool> CreateCode()
{
return false;
}

int main()
{
ErrorCode<bool> result = CreateCode();

return 0;
}


gcc says this:
main.cpp: In function `int main()':
main.cpp:71: error: conversion from `ThrowableErrorCode<bool>' to
non-scalar type `ErrorCode<bool>' requested

What is wrong with the constructor in ErrorCode?

--
Daniel

 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      10-12-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello!
>
> The following code compiles with msvc-7.1, but gcc-3.4.4 fails. I fail
> too, in seeing why.
>


It doesn't compile on VC++8 or Comeau. Haven't tried it on 7.1.

You can get around it by doing the following:

>
> #include <iostream>
> #include <string>
> #include <stdexcept> // logic_error
>
> struct IgnoreErrorCode
> {};
>
> struct MandatoryErrorCodeException : public std::logic_error
> {
> MandatoryErrorCodeException(const char* s)
> : std::logic_error(s)
> { }
> };
>
> template<class CODE> class ErrorCode;
>
> template<class CODE>
> class ThrowableErrorCode
> {
> friend class ErrorCode<CODE>;
> CODE m_code;
> bool m_throw;


Make this "mutable":
mutable bool m_throw;

> public:
> //! constructor - receive the error code and arm the exception
> ThrowableErrorCode(CODE i_code)
> : m_code(i_code),
> m_throw(true)
> {}
>
> //! explicitly ignore the error code and avoid exception
> operator IgnoreErrorCode()
> {
> m_throw = false;
> return IgnoreErrorCode();
> }
> ~ThrowableErrorCode()
> {
> //! will throw unless ErrorCode<CODE> or IgnoreError will prevent
> it
> if( m_throw )
> throw MandatoryErrorCodeException("Must handle error code");
> }
> };
>
> template<class T>
> class ErrorCode
> {
> T m_code;
>
> public:
>
> ErrorCode(ThrowableErrorCode<T>& code)


And the parameter here const:
ErrorCode(const ThrowableErrorCode<T>& code)


> : m_code(code.m_code)
> {
> // prevent the throw
> code.m_throw = false;
> }
> operator T()
> {
> return this->m_code;
> }
> };
>
> ThrowableErrorCode<bool> CreateCode()
> {
> return false;
> }
>
> int main()
> {
> ErrorCode<bool> result = CreateCode();
>
> return 0;
> }
>


Just one possibility. You need to decide whether making m_throw a
mutable member is consistent with the way you have conceptualized your
solution.

Regards,
Sumit.
 
Reply With Quote
 
 
 
 
dlidstrom@gmail.com
Guest
Posts: n/a
 
      10-12-2006
Sumit Rajan wrote:
> Just one possibility. You need to decide whether making m_throw a
> mutable member is consistent with the way you have conceptualized your
> solution.


Thanks! This solution works for me.

--
Daniel

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
how to call an inherited, template class constructor from initializerlist of an inheriting, non-template class constructor l.s.rockfan@web.de C++ 4 11-15-2008 01:22 PM
how to call an inherited, template class constructor from initializerlist of an inheriting, non-template class constructor l.s.rockfan@web.de C++ 2 11-14-2008 10:04 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
template constructor in non-template class Alexander Stippler C++ 2 12-01-2004 04:08 PM



Advertisments