@@ -1216,6 +1216,7 @@ template <typename CharT> struct type_caster<CharT, enable_if_t<is_std_char_type
1216
1216
using StringCaster = type_caster<StringType>;
1217
1217
StringCaster str_caster;
1218
1218
bool none = false ;
1219
+ CharT one_char = 0 ;
1219
1220
public:
1220
1221
bool load (handle src, bool convert) {
1221
1222
if (!src) return false ;
@@ -1243,7 +1244,7 @@ template <typename CharT> struct type_caster<CharT, enable_if_t<is_std_char_type
1243
1244
}
1244
1245
1245
1246
operator CharT*() { return none ? nullptr : const_cast <CharT *>(static_cast <StringType &>(str_caster).c_str ()); }
1246
- operator CharT () {
1247
+ operator CharT& () {
1247
1248
if (none)
1248
1249
throw value_error (" Cannot convert None to a character" );
1249
1250
@@ -1267,7 +1268,8 @@ template <typename CharT> struct type_caster<CharT, enable_if_t<is_std_char_type
1267
1268
if (char0_bytes == str_len) {
1268
1269
// If we have a 128-255 value, we can decode it into a single char:
1269
1270
if (char0_bytes == 2 && (v0 & 0xFC ) == 0xC0 ) { // 0x110000xx 0x10xxxxxx
1270
- return static_cast <CharT>(((v0 & 3 ) << 6 ) + (static_cast <unsigned char >(value[1 ]) & 0x3F ));
1271
+ one_char = static_cast <CharT>(((v0 & 3 ) << 6 ) + (static_cast <unsigned char >(value[1 ]) & 0x3F ));
1272
+ return one_char;
1271
1273
}
1272
1274
// Otherwise we have a single character, but it's > U+00FF
1273
1275
throw value_error (" Character code point not in range(0x100)" );
@@ -1278,19 +1280,20 @@ template <typename CharT> struct type_caster<CharT, enable_if_t<is_std_char_type
1278
1280
// surrogate pair with total length 2 instantly indicates a range error (but not a "your
1279
1281
// string was too long" error).
1280
1282
else if (StringCaster::UTF_N == 16 && str_len == 2 ) {
1281
- char16_t v0 = static_cast <char16_t >(value[0 ]);
1282
- if (v0 >= 0xD800 && v0 < 0xE000 )
1283
+ one_char = static_cast <CharT >(value[0 ]);
1284
+ if (one_char >= 0xD800 && one_char < 0xE000 )
1283
1285
throw value_error (" Character code point not in range(0x10000)" );
1284
1286
}
1285
1287
1286
1288
if (str_len != 1 )
1287
1289
throw value_error (" Expected a character, but multi-character string found" );
1288
1290
1289
- return value[0 ];
1291
+ one_char = value[0 ];
1292
+ return one_char;
1290
1293
}
1291
1294
1292
1295
static PYBIND11_DESCR name () { return type_descr (_ (PYBIND11_STRING_NAME)); }
1293
- template <typename _T> using cast_op_type = remove_reference_t < pybind11::detail::cast_op_type<_T> >;
1296
+ template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
1294
1297
};
1295
1298
1296
1299
// Base implementation for std::tuple and std::pair
0 commit comments