File tree 1 file changed +25
-5
lines changed 1 file changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,16 @@ namespace tao::pq
34
34
class table_writer ;
35
35
class transaction ;
36
36
37
+ namespace internal
38
+ {
39
+ template < typename >
40
+ inline constexpr bool is_optional = false ;
41
+
42
+ template < typename T >
43
+ inline constexpr bool is_optional< std::optional< T > > = true ;
44
+
45
+ } // namespace internal
46
+
37
47
class result final
38
48
{
39
49
private:
@@ -226,21 +236,31 @@ namespace tao::pq
226
236
[[nodiscard]] auto at ( const std::size_t row ) const -> pq::row;
227
237
228
238
template < typename T >
239
+ requires internal::is_optional< T >
229
240
[[nodiscard]] auto as () const -> T
230
241
{
242
+ if ( empty () ) {
243
+ return std::nullopt;
244
+ }
231
245
if ( size () != 1 ) {
232
- throw std::runtime_error ( std::format ( " invalid result size: {} rows, expected 1 row " , m_rows ) );
246
+ throw std::runtime_error ( std::format ( " invalid result size: {} rows, expected 0 or 1 rows " , m_rows ) );
233
247
}
234
248
return ( *this )[ 0 ].as < T >();
235
249
}
236
250
237
251
template < typename T >
238
- [[nodiscard]] auto optional () const -> std::optional< T >
252
+ [[nodiscard]] auto as () const -> T
239
253
{
240
- if ( empty () ) {
241
- return std::nullopt ;
254
+ if ( size () != 1 ) {
255
+ throw std::runtime_error ( std::format ( " invalid result size: {} rows, expected 1 row " , m_rows ) ) ;
242
256
}
243
- return as< T >();
257
+ return ( *this )[ 0 ].as < T >();
258
+ }
259
+
260
+ template < typename T >
261
+ [[nodiscard]] auto optional () const
262
+ {
263
+ return as< std::optional< T > >();
244
264
}
245
265
246
266
template < typename T, typename U >
You can’t perform that action at this time.
0 commit comments