Problem /w constructor in template

Discussion in 'C++' started by dlidstrom@gmail.com, Oct 12, 2006.

  1. Guest

    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
     
    , Oct 12, 2006
    #1
    1. Advertising

  2. Sumit Rajan Guest

    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.
     
    Sumit Rajan, Oct 12, 2006
    #2
    1. Advertising

  3. Guest

    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, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Stippler

    template constructor in non-template class

    Alexander Stippler, Dec 1, 2004, in forum: C++
    Replies:
    2
    Views:
    9,727
    Tom Widmer
    Dec 1, 2004
  2. benben
    Replies:
    1
    Views:
    420
    Alf P. Steinbach
    Feb 15, 2006
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,254
  4. cinsk
    Replies:
    35
    Views:
    2,620
    James Kanze
    Oct 11, 2010
  5. Replies:
    1
    Views:
    295
Loading...

Share This Page