現在、const または non-const 参照として複雑な構造の内部要素へのアクセスを提供するインターフェイス クラスを作成しています。一部のモジュールには const アクセスが許可され、一部のモジュールにはフル アクセスが許可されるという考え方です。
「type_traits」「std::add_const」を使用して、内部メンバー関数の戻り値の型を条件付きで修飾しましたが、残念ながら、メンバー関数を const または非 const として条件付きで修飾する方法は考えられません。
これは可能ですか?もしそうならどのように?
例えば:
template< typename T, bool isConst >
struct apply_const
{
typedef T type;
};
template<typename T>
struct apply_const<T, true>
{
typedef typename std::add_const<T>::type type;
};
template< bool isConst >
const Interface
{
/// @brief get the TypeA member
typename apply_const<TypeA, isConst >::type& GetXpo3Container() // how do I conditionally add a const qualifier
{
return config_.type_a_member_;
}
typename apply_const<Profile, isConst >::type& GetProfile( unint32_t id ) // qualifier ???
{
return config_.profiles.get( id );
}
// .... lots more access functions
ConfigType config_; // the config
};
注: インターフェイスの 2 つのバージョンを分離/作成する根本的な理由は、それらが異なるインスタンスconfig
(書き込み可能なものとそうでないもの) へのアクセスを提供することです。開発中のサブシステムは<running>
、<candidate>
構成をサポートする組み込みの Netconf エージェントです。