473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Problem /w constructor in template

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 CODEclass 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<CODEor 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<boolCreateCode()
{
return false;
}

int main()
{
ErrorCode<boolresult = 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

Oct 12 '06 #1
2 1370
dl*******@gmail.com 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 CODEclass 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<CODEor 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<boolCreateCode()
{
return false;
}

int main()
{
ErrorCode<boolresult = 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.
Oct 12 '06 #2
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

Oct 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gary | last post by:
Hi, I am a Chinese student, I have a problem with the following code //The follwing code in StaticSearch.h: // template <class Type> class dataList; // template <class Type> class Node ...
3
by: Gandu | last post by:
Could some C++ guru please help me. I have a template based general linked list class that I want to inherit publicly to create a queue class. In the case of non-template inheritance, I can use:...
2
by: max_sang | last post by:
Hello I have a nasty problem... take a look at this code: struct Parser { Parser(const string& s) { ... tokenizes s into pieces... } template <class T> to(size_t idx); private: vector<string>...
6
by: B. Williams | last post by:
I have a problem dealing with class template where I was to write a class and after submitting the class, this is the feedback I got back from the instructor. I don't really understand it. Can...
2
by: Korosov | last post by:
Hi! GCC (4.1.2 20070502 (Red Hat 4.1.2-12)) gives an error: test.cpp: In constructor <Dclass<T>::Dclass()>: test.cpp:27: error: <a> was not declared in this scope when compiling the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.