1

この質問は私のものに基づいています:

C++ ライブラリ: .hpp + .inl (別個の定義と宣言) と .hpp のみ (クラス内の本体コード)

テンプレート化された関数をインライン ファイルに移動することは、間違いなく公式の地獄です。以下の 3 つの例を参照してください。私の昔ながらのきちんとした方法 (#0) から、本当に拡張された方法 (#1)、よりコンパクトな方法 (#2)、そして想像上の夢のパイプの方法 (#3) まで。

// #0 the actual class definition
namespace CA {
    template <typename tnChar>
    class MyClass {
    public:
        typedef tnChar                      tChar;
        typedef std::basic_string<tChar>    tString;
        MyClass(); // {}
        tString Method1(const tString& aParam) const; // { return aParam; }
    };
};

これはインラインバージョン #1です。名前空間とクラスの両方があります。

template <typename tnChar>
CA::MyClass<tnChar>::MyClass(){}

template <typename tnChar>
typename CA::MyClass<tnChar>::tString
CA::MyClass<tnChar>::Method1(const tString& aParam) const {
    return aParam;
}

template <> // specializing
typename CA::MyClass<wchar_t>::tString
CA::MyClass<wchar_t>::Method1(const tString& aParam) const {
    return aParam;
}

これはインラインバージョン #2です。名前空間にラップされます。の追加から保存されましたCA::

namespace CA {
    template <typename tnChar>
    MyClass<tnChar>::MyClass(){}

    template <typename tnChar>
    typename MyClass<tnChar>::tString
    MyClass<tnChar>::Method1(const tString& aParam) const {
        return aParam;
    }

    template <> // specializing
    typename MyClass<wchar_t>::tString
    MyClass<wchar_t>::Method1(const tString& aParam) const {
        return aParam;
    }
}

これはインラインバージョン #3です。名前空間とクラスにラップされます。template <...>...の追加から保存されましたCA::MyClass架空のバージョン。不可能だが望ましい!

#if 0 // like this is ever gonna work
// inline version #3 (IMAGINARY)
// wrapped in namespace and in class
// 'inline class' should make this functionality happen in C++my
namespace CA {
    template <typename tnChar>
    inline class MyClass { // :)
        MyClass(){}

        tString Method1(const tString& aParam) const {
            return aParam;
        }
    };

    template <>
    inline class MyClass<wchar_t> { // :) - too much imagination
        tString Method1(const tString& aParam) const {
            return aParam;
        }
    };
}
#endif // disabled

私の質問は:バージョン 3 のようなものはリモートでも可能ですか? typename戻り値をいちいちいちいち書き込む必要がありませんtemplate <...>。なぜこれを行う必要があるのか​​ はわかっていますが、新しいC++11ものや次のものがテンプレートクラスのインラインメソッドグループ化C++14に対処する計画があるかどうか疑問に思っています?

#3でキーワードが架空の機能をinline classトリガーすることは驚くべきことであり、テンプレートクラスメソッドの外部化が非常に簡単になります...そしてロジック...そしてグループ化されます...そして短い:)

4

1 に答える 1

1

明白なことを述べるリスクがありますが、その構文は、クラス本体内でメンバー関数を宣言するときに使用できます。宗教上、メンバー関数を別の.inlファイルに入れる必要がある場合は、そのファイルをクラス本体自体に含めることができます。

于 2013-08-05T18:31:21.523 に答える