• macro-problems

    From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c++ on Thu Feb 25 07:24:47 2021
    From Newsgroup: comp.lang.c++

    I've got a header with a macro to define new exception-subclasses:

    #pragma once
    #include <exception>
    #include <string>

    #define exc_subclass(base, deriv, default_what) \
    struct deriv : public base \
    { \
    deriv( char const *what = default_what ) noexcept : \
    m_what( what ) \
    { \
    } \
    deriv( exception_ptr nextException, char const *what = default_what ) noexcept : \
    m_nextException( nextException ), \
    m_what( what ) \
    { \
    } \
    deriv( deriv const &other ) noexcept : \
    base( other ), \
    m_what( other.what() ) \
    { \
    } \
    virtual char const *what() const noexcept override \
    { \
    return m_what.c_str(); \
    } \
    std::exception_ptr next_exception() const noexcept \
    { \
    return m_nextException; \
    } \
    private: \
    std::string m_what; \
    std::exception_ptr m_nextException; \
    };

    But this doesn't accept ...
    exc_subclass(std::exception, deriv, "my default what")
    ... because of the :: in it.
    Is there any escape for :: ?
    --- Synchronet 3.18c-Linux NewsLink 1.113
  • From Ben Bacarisse@ben.usenet@bsb.me.uk to comp.lang.c++ on Thu Feb 25 11:11:31 2021
    From Newsgroup: comp.lang.c++

    Bonita Montero <Bonita.Montero@gmail.com> writes:

    I've got a header with a macro to define new exception-subclasses:

    #pragma once
    #include <exception>
    #include <string>

    #define exc_subclass(base, deriv, default_what) \
    struct deriv : public base \
    { \
    deriv( char const *what = default_what ) noexcept : \
    m_what( what ) \
    { \
    } \
    deriv( exception_ptr nextException, char const *what = default_what ) noexcept : \
    m_nextException( nextException ), \
    m_what( what ) \
    { \
    } \
    deriv( deriv const &other ) noexcept : \
    base( other ), \
    m_what( other.what() ) \
    { \
    } \
    virtual char const *what() const noexcept override \
    { \
    return m_what.c_str(); \
    } \
    std::exception_ptr next_exception() const noexcept \
    { \
    return m_nextException; \
    } \
    private: \
    std::string m_what; \
    std::exception_ptr m_nextException; \
    };

    But this doesn't accept ...
    exc_subclass(std::exception, deriv, "my default what")
    ... because of the :: in it.

    The macro does not care about the ::. If it does not work, it's for
    some other reason.
    --
    Ben.
    --- Synchronet 3.18c-Linux NewsLink 1.113
  • From Paavo Helde@myfirstname@osa.pri.ee to comp.lang.c++ on Thu Feb 25 13:43:27 2021
    From Newsgroup: comp.lang.c++

    25.02.2021 08:24 Bonita Montero kirjutas:
    I've got a header with a macro to define new exception-subclasses:

    #pragma once
    #include <exception>
    #include <string>

    #define exc_subclass(base, deriv, default_what) \
    struct deriv : public base \
    { \
        deriv( char const *what = default_what ) noexcept : \
            m_what( what ) \
        { \
        } \
        deriv( exception_ptr nextException, char const *what = default_what

    You are missing std:: before exception_ptr in the prev line.

    Colons in preprocessor macros are fine. Commas, on the other hand, might
    be a bit tricky sometimes.



    --- Synchronet 3.18c-Linux NewsLink 1.113
  • From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c++ on Thu Feb 25 14:54:39 2021
    From Newsgroup: comp.lang.c++

    You are missing std:: before exception_ptr in the prev line.

    Ok, that's what someone else found on Stack Overflow also.
    But thanks.

    --- Synchronet 3.18c-Linux NewsLink 1.113