テンプレートパラメータとして指定されたクラスのメソッドgetGG()のClass2戻り値型をチェックインしようとしましたが、コードがコンパイルされません。それを正しく行う方法は?
template <class T, class U>
struct hasProperMethodReturnValueType {
static constexpr bool value = std::is_same<T, std::decltype(U.getGG())>;
};
template<class P> class Class1 {
private:
P gg;
public:
Class1(P a) : gg(a) {}
P getGG() {
return gg;
}
};
template<class A, class P> class Class3 {
private:
P gg;
A dd;
public:
Class3(P a, A r) : gg(a), dd(r) {}
P getGG() {
return gg;
}
};
template<class G, class R> class Class2 {
static_assert(hasProperMethodReturnValueType<G, R>::value, "Not same type");
private:
R cc;
public:
Class2(R r) : cc(r) {};
};
int main() {
auto obj = Class2<int, Class1<int> >(Class1<int>(3));
auto obj2 = Class2<int, Class3<float, int> >(Class3<float, int>(0, 1.1));
return 0;
}
コンパイルエラー:
error: template argument 2 is invalid
static constexpr bool value = std::is_same<T, std::decltype(U.getGG())>;
を使用しますstd::declval
。
template <class T, class U>
struct hasProperMethodReturnValueType
: std::is_same<T, decltype(std::declval<U>().getGG())>
{};
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加