5

std::runtime_errorストリーミングのサポートを例外に追加するために、から例外クラスを派生させました。解決方法がわからない、clang で奇妙なコンパイラ エラー出力が表示されます。

clang++ -std=c++11 -stdlib=libc++ -g -Wall -I../ -I/usr/local/include Main.cpp -c
Main.cpp:43:19: error: call to deleted constructor of 'EarthException'
            throw EarthException(__FILE__, __LINE__)
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../EarthException.hpp:9:12: note: function has been explicitly marked deleted here
    struct EarthException : public Exception<EarthException>


template <typename TDerived>
    class Exception : public std::runtime_error
    {
        public:
            Exception() : std::runtime_error("") {}

            Exception(const std::string& file, const unsigned line)
                 : std::runtime_error("")
            { 
                stream_ << (file.empty() ? "UNNAMED_FILE" : file) << "[" << line << "]: ";
            }

            virtual ~Exception() {}

            template <typename T>
            TDerived& operator<<(const T& t)
            {
                stream_ << t;
                return static_cast<TDerived&>(*this);
            }

            virtual const char* what() const throw()
            {
                return stream_.str().c_str();
            }

       private:
           std::stringstream stream_;
    };

    struct EarthException : public Exception<EarthException>
    {
        EarthException() {}

        EarthException(const std::string& file, const unsigned line)
            : Exception<EarthException>(file, line) {}

        virtual ~EarthException() {}
    };
}

アップデート:

std::runtime_error("")これのデフォルトのコンストラクタが指摘されたので、明示的な呼び出しを追加しました=deleteが、エラーは残っています。

4

2 に答える 2

6

と でユーザーが宣言したデストラクタがあるため、これらのクラスのムーブ コンストラクターExceptionEarthExceptionムーブ代入演算子の暗黙的な生成は無効になっています。また、移動専用のデータ メンバーのためstd::stringstream、暗黙的なコピー メンバーが削除されます。

しかし、それはすべて気を散らすものです。

あなたのwhatメンバーは運命にある:

        virtual const char* what() const throw()
        {
            return stream_.str().c_str();
        }

これにより、右辺値が作成std::stringされ、その一時へのポインターが返されます。クライアントがポインターを一時的に読み取る前に、一時的に破棄されます。

あなたがする必要があるのは、基本クラスstd::stringにダウンを渡すことです。、またはその他のデータメンバーstd::runtime_errorを保持する必要はありません。stringstream唯一のトリッキーな部分はstd::runtime_error、適切な で基本クラスを初期化することstringです:

template <typename TDerived>
    class Exception : public std::runtime_error
    {
            static
            std::string
            init(const std::string& file, const unsigned line)
            {
                std::ostringstream os;
                os << (file.empty() ? "UNNAMED_FILE" : file) << "[" << line << "]: ";
                return os.str();
            }
        public:
            Exception(const std::string& file, const unsigned line)
                : std::runtime_error(init(file, line))
            { 
            }

      private:
          <del>std::stringstream stream_;</del>

これで、暗黙的なコピー メンバーを取得でき、問題なく機能します。

于 2012-08-25T16:55:50.160 に答える
1
Exception(const std::string& file, const unsigned line)
            { 
                stream_ << (file.empty() ? "UNNAMED_FILE" : file) << "[" << line << "]: ";
            }

このコンストラクターはその基本コンストラクターを呼び出さないため、コンパイラーは既定のコンストラクターへの呼び出しを生成しますstd::runtime_error::runtime_error()。しかしstd::runtime_error、デフォルトのコンストラクターがありません。これは、エラー メッセージが示していることです。これを修正するstd::runtime_errorには、コンストラクターの 1 つについて読んで呼び出します。

編集: わかりました、ここに本当の問題があります (上記で説明したことも問題ではないということではありません): テンプレートExceptionには type のデータ メンバーがありstd::stringstreamます。ストリームはコピーできないため、コンパイラはスローに使用するコピー コンストラクターを生成できません。

于 2012-08-25T15:18:22.553 に答える