Skip to content

非リテラル型な任意のUDT(User-Defined Type)に対して、constexprコピーコンストラクタはill-formedになる #22

@yumetodo

Description

@yumetodo

以前某中3女子に質問をしたことがあって、

https://gcc.gnu.org/ml/libstdc++/2012-10/msg00120.html

diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono
index 209f395..d920a7d 100644
--- a/libstdc++-v3/include/std/chrono
+++ b/libstdc++-v3/include/std/chrono
@@ -250,7 +250,10 @@ _GLIBCXX_END_NAMESPACE_VERSION
    // 20.11.5.1 construction / copy / destroy
    constexpr duration() = default;

-   constexpr duration(const duration&) = default;
+   // NB: Make constexpr implicit. This cannot be explicitly
+   // constexpr, as any UDT that is not a literal type with a
+   // constexpr copy constructor will be ill-formed.
+   duration(const duration&) = default;

    template<typename _Rep2, typename = typename
           enable_if<is_convertible<_Rep2, rep>::value

という変更がどういう意味かを問い合わせたことがありました。

@yumetodo 直訳:暗黙にconstexpr指定される。非リテラル型な任意のUDT(User-Defined Type)に対して、constexprコピーコンストラクタはill-formedになるため、明示的にconstexpr指定すべきでない。

— 狂える中3女子ボレロ村上/陶芸C++er (@bolero_MURAKAMI) 2016年3月14日

@yumetodo つまり、duration<Rep> の Rep は非リテラル型なユーザ定義型である可能性があるため、明示的にconstexpr指定しないようにする変更。Rep がリテラル型である場合、トリビアルコピーコンストラクタは暗黙にconstexpr指定される。

— 狂える中3女子ボレロ村上/陶芸C++er (@bolero_MURAKAMI) 2016年3月14日

と回答していただきました。

さて、例えば

		template<typename Int_>
		struct Coordinate1Dimensional {
			Int_ x{};
			constexpr Coordinate1Dimensional() = default;
			constexpr Coordinate1Dimensional(const Int_& x_) noexcept :x(x_) {};
		};

をみると、上と全く同じ状況であることがわかります。

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions