• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    concept
    <concepts>

    std::constructible_from

    namespace std {
      template <class T, class... Args>
      concept constructible_from = destructible<T> && is_constructible_v<T, Args...>;
    }
    

    概要

    constructible_fromは、任意の型Tが引数型Args...から構築可能であることを表すコンセプトである。

    #include <iostream>
    #include <concepts>
    #include <vector>
    #include <optional>
    #include <memory>
    #include <span>
    
    template<typename T, typename... Args>
    requires std::constructible_from<T, Args...>
    void f(const char* name, const char* args) {
      std::cout << name << " is constructible from " << args << std::endl;
    }
    
    template<typename T, typename... Args>
    void f(const char* name, const char* args) {
      std::cout << name << " is not constructible from " << args << std::endl;
    }
    
    
    int main() {
      f<std::vector<int>, std::initializer_list<int>>("std::vector<int>", "std::initializer_list<int>");
      f<std::optional<std::vector<int>>, std::in_place_t, std::initializer_list<int>, std::allocator<int>>("std::optional<std::vector<int>>", "std::in_place_t, std::initializer_list<int>, std::allocator<int>");
      f<std::unique_ptr<const int>, std::unique_ptr<int>>("std::unique_ptr<const int>", "std::unique_ptr<int>");
      f<std::span<int, 4>, std::array<int, 4>&>("std::span<int, 4>", "std::array<int, 4>&");
    
      std::cout << "\n";
    
      f<std::vector<int>, std::vector<double>>("std::vector<int>", "std::vector<double>");
      f<std::optional<std::vector<int>>, std::optional<std::vector<double>>>("std::optional<std::vector<int>>", "std::optional<std::vector<double>>");
      f<std::unique_ptr<int>, std::unique_ptr<const int>>("std::unique_ptr<int>", "std::unique_ptr<const int>");
      f<std::span<int, 4>, std::array<int, 4>>("std::span<int, 4>", "std::array<int, 4>");
    }
    

    出力

    std::vector<int> is constructible from std::initializer_list<int>
    std::optional<std::vector<int>> is constructible from std::in_place_t, std::initializer_list<int>, std::allocator<int>
    std::unique_ptr<const int> is constructible from std::unique_ptr<int>
    std::span<int, 4> is constructible from std::array<int, 4>&
    
    std::vector<int> is not constructible from std::vector<double>
    std::optional<std::vector<int>> is not constructible from std::optional<std::vector<double>>
    std::unique_ptr<int> is not constructible from std::unique_ptr<const int>
    std::span<int, 4> is not constructible from std::array<int, 4>
    

    バージョン

    言語

    • C++20

    処理系

    関連項目

    参照