0

完全な転送の候補であるテンプレート化されたコンストラクターを備えたこの(おそらくあまり役​​に立たない)クラステンプレートがあります。ただし、コンストラクターに渡される型がクラス全体に指定されたものとまったく同じであることを確認したかった (cvref 修飾子なし):

template <typename... Ts>
struct foo {
    template <typename... CTs>
    requires (std::same_as<std::remove_cvref_t<Ts>, std::remove_cvref_t<CTs>> && ...)
    foo(CTs&& ...) {
        std::cout << "called with " << (sizeof...(CTs)) << " args\n";
    }
};

今私は作ることができます:

auto f = foo<int, int>(1, 1);

そして私は作ることができません:

auto f = foo<float, int>(1, 1);

どっちがいい。


requires ...しかし、私は体をに抽出したかったconcept:

template <typename... T1s, typename... T2s>
concept same_unqualified_types = (
        std::same_as<
                std::remove_cvref_t<T1s>,
                std::remove_cvref_t<T2s>
        > && ...
);

template <typename... Ts>
struct foo {
    template <typename... CTs>
    requires same_unqualified_types<Ts..., CTs...>
    foo(CTs&& ...) {                // 16
        std::cout << "called with " << (sizeof...(CTs)) << " args\n";
    }
};

int main() {
    auto f = foo<int, int>(1, 1);   // 22
}

しかし、これは私にこのエラーを与えます:

main.cpp: In function 'int main()': 
main.cpp:22:32: error: no matching function for call to 'foo<int, int>::foo(int, int)'
22 |     auto f = foo<int, int>(1, 1);
   |   
main.cpp:16:5: note: candidate: 'template<class ... CTs>  requires  same_unqualified_types<Ts ..., CTs ...> foo<Ts>::foo(CTs&& ...) [with CTs = {CTs ...}; Ts = {int, int}]'
16 |     foo(CTs&& ...) {
   |     ^~~
main.cpp:16:5: note:   template argument deduction/substitution failed:
main.cpp:16:5: note: constraints not satisfied
main.cpp: In substitution of 'template<class ... CTs>  requires  same_unqualified_types<Ts ..., CTs ...> foo<int, int>::foo(CTs&& ...) [with CTs = {int, int}]':
main.cpp:22:32:   required from here
main.cpp:5:9:   required for the satisfaction of 'same_unqualified_types<Ts ..., CTs ...>' [with CTs = {int, int}; Ts = {int, int}]
main.cpp:22:32: error: mismatched argument pack lengths while expanding 'same_as<typename std::remove_cvref<T1s>::type, typename std::remove_cvref<T2s>::type>'
22 |     auto f = foo<int, int>(1, 1);
   |    

concept same_unqualified_types2 つのパラメーター パックを使用しようとしているところに何か問題があるのではないかと思います。手動でテストしようとしましたが、same_unqualified_types<int, int>orsame_unqualified_types<int, int, Pack...>を実行しても機能しないようです。Pack...int

私のロジックはどこに欠陥がありますか? requiresその句をに抽出できますconceptか?

免責事項: CTAD と演繹ガイドを使用して、概念を必要とせずに同様のことを達成できることを私は知っています。私の理解がどこに欠けているのか知りたいだけです。

4

1 に答える 1