[gcc r12-8935] libstdc++: Check static assertions earlier in chrono::duration

Jonathan Wakely redi@gcc.gnu.org
Thu Nov 24 16:36:26 GMT 2022


https://gcc.gnu.org/g:d9b0e012ab939568f5ccbefc4a68ba515e66f4b5

commit r12-8935-gd9b0e012ab939568f5ccbefc4a68ba515e66f4b5
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Nov 21 11:52:34 2022 +0000

    libstdc++: Check static assertions earlier in chrono::duration
    
    This ensures that we fail a static assertion before giving any other
    errors. Instantiating chrono::duration<int, chrono::seconds> will now
    print this before the other errors caused by it:
    
    error: static assertion failed: period must be a specialization of ratio
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/chrono.h (duration): Check preconditions on
            template arguments before using them.
    
    (cherry picked from commit ed77dcb9be76e592b62449c75a5e751485514afd)

Diff:
---
 libstdc++-v3/include/bits/chrono.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/libstdc++-v3/include/bits/chrono.h b/libstdc++-v3/include/bits/chrono.h
index 421898516ae..65e3e3183f7 100644
--- a/libstdc++-v3/include/bits/chrono.h
+++ b/libstdc++-v3/include/bits/chrono.h
@@ -442,6 +442,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     template<typename _Rep, typename _Period>
       struct duration
       {
+	static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
+	static_assert(__is_ratio<_Period>::value,
+		      "period must be a specialization of ratio");
+	static_assert(_Period::num > 0, "period must be positive");
+
       private:
 	template<typename _Rep2>
 	  using __is_float = treat_as_floating_point<_Rep2>;
@@ -487,11 +492,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	using rep = _Rep;
 	using period = typename _Period::type;
 
-	static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
-	static_assert(__is_ratio<_Period>::value,
-		      "period must be a specialization of ratio");
-	static_assert(_Period::num > 0, "period must be positive");
-
 	// 20.11.5.1 construction / copy / destroy
 	constexpr duration() = default;


More information about the Libstdc++-cvs mailing list