Skip to content

Commit 9c12931

Browse files
authored
[clang][test] Avoid some C++14 warnings in discrim-union.cpp (#146829)
Before this patch, we emitted a bunch of irrelevant (to this test) warnings: ``` ../clang/test/SemaCXX/discrim-union.cpp:49:24: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 49 | constexpr const T &get(select<0>) { return val; } | ^ | const ../clang/test/SemaCXX/discrim-union.cpp:50:104: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 50 | template<unsigned N> constexpr const decltype(static_cast<const rest_t&>(rest).get(select<N-1>{})) get(select<N>) { | ^ | const ../clang/test/SemaCXX/discrim-union.cpp:82:22: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 82 | constexpr unsigned index() noexcept { return elem; } | ^ | const ../clang/test/SemaCXX/discrim-union.cpp:88:33: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 88 | constexpr const_get_result<N> get() { | ^ | const ../clang/test/SemaCXX/discrim-union.cpp:96:22: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const] 96 | constexpr const U &get() { | ^ | const 5 warnings generated. ```
1 parent ed1ee9a commit 9c12931

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

clang/test/SemaCXX/discrim-union.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: %clang_cc1 -std=c++11 %s -fsyntax-only -fcxx-exceptions
1+
// RUN: %clang_cc1 -std=c++11 %s -fsyntax-only -fcxx-exceptions -verify
2+
3+
// expected-no-diagnostics
24

35
template<typename T> struct remove_reference { typedef T type; };
46
template<typename T> struct remove_reference<T&> { typedef T type; };
@@ -46,8 +48,8 @@ namespace detail {
4648
val.~T();
4749
}
4850

49-
constexpr const T &get(select<0>) { return val; }
50-
template<unsigned N> constexpr const decltype(static_cast<const rest_t&>(rest).get(select<N-1>{})) get(select<N>) {
51+
constexpr const T &get(select<0>) const { return val; }
52+
template<unsigned N> constexpr const decltype(static_cast<const rest_t&>(rest).get(select<N-1>{})) get(select<N>) const {
5153
return rest.get(select<N-1>{});
5254
}
5355
};
@@ -79,21 +81,21 @@ class either {
7981
// FIXME: declare a destructor iff any element has a nontrivial destructor
8082
//~either() { impl.destroy(elem); }
8183

82-
constexpr unsigned index() noexcept { return elem; }
84+
constexpr unsigned index() const noexcept { return elem; }
8385

8486
template<unsigned N> using const_get_result =
8587
decltype(static_cast<const impl_t&>(impl).get(detail::select<N>{}));
8688

8789
template<unsigned N>
88-
constexpr const_get_result<N> get() {
90+
constexpr const_get_result<N> get() const {
8991
// Can't just use throw here, since that makes the conditional a prvalue,
9092
// which means we return a reference to a temporary.
9193
return (elem != N ? throw_<const_get_result<N>>("bad_either_get")
9294
: impl.get(detail::select<N>{}));
9395
}
9496

9597
template<typename U>
96-
constexpr const U &get() {
98+
constexpr const U &get() const {
9799
return get<impl_t::index(detail::type<U>())>();
98100
}
99101
};

0 commit comments

Comments
 (0)