gpt4 book ai didi

c++ - 停止编译器建议重载模板的候选,在 static_assert 中扩展 PRETTY_FUNCTION

转载 作者:行者123 更新时间:2023-12-01 14:47:37 25 4
gpt4 key购买 nike

我的代码大量使用模板。我有许多重载的函数

auto operator>>(byte_vector_view&bvv, Type&&) ->byte_vector_view&;

这工作正常,直到此函数系列类型未知传递给函数:
bvv >> my_custom;

用户应该只看到一条错误消息:

There no implementation for operator>>(byte_vector_view&bvv, my_custom_type&)



相反,我们看到了一个很长的手稿:

如何把这个放在剧透下?
itm.cpp:184:18: error: invalid operands to binary expression ('byte_vector_view' and 'named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >')
((bvv>>elements),...);
~~~^ ~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/type_traits:4345:23: note: in instantiation of function template specialization 'operator>>(byte_vector_view &, std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &)::(anonymous class)::operator()<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
_LIBCPP_INVOKE_RETURN(_VSTD::forward<_Fp>(__f)(_VSTD::forward<_Args>(__args)...))
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/__config:508:15: note: expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1375:5: note: while substituting deduced template arguments into function template '__invoke_constexpr' [with _Fp = (lambda at itm.cpp:183:9), _Args = <named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > &>]
_VSTD::__invoke_constexpr(
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/__config:508:15: note: expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_NAMESPACE
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1372:26: note: in instantiation of exception specification for '__apply_tuple_impl<(lambda at itm.cpp:183:9), std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &, 0>' requested here
constexpr decltype(auto) __apply_tuple_impl(_Fn && __f, _Tuple && __t,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1384:12: note: in instantiation of function template specialization 'std::__1::__apply_tuple_impl<(lambda at itm.cpp:183:9), std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &, 0>' requested here
_VSTD::__apply_tuple_impl(
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1382:26: note: in instantiation of exception specification for 'apply<(lambda at itm.cpp:183:9), std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &>' requested here
constexpr decltype(auto) apply(_Fn && __f, _Tuple && __t)
^
itm.cpp:182:5: note: in instantiation of function template specialization 'std::__1::apply<(lambda at itm.cpp:183:9), std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > &>' requested here
apply(
^
itm.cpp:461:20: note: in instantiation of function template specialization 'operator>><std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > >' requested here
bvv>>tvs;
^
itm.cpp:427:12: note: in instantiation of member function 'make_converter(bool, std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> > &&, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > &&)::Ret::from_byte_vector_view' requested here
struct Ret:converter_virtual{
^
itm.cpp:537:12: note: in instantiation of function template specialization 'make_converter<std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> >, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
return make_converter(false,forward<Prefix>(prefix),forward<Ts>(values)...);
^
itm.cpp:931:18: note: in instantiation of function template specialization 'make_converter<std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> >, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
/*response*/make_converter(
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/type_traits:4840:3: note: candidate function template not viable: no known conversion from 'byte_vector_view' to 'std::byte' for 1st argument
operator>> (byte __lhs, _Integer __shift) noexcept
^
itm.cpp:147:19: note: candidate function not viable: no known conversion from 'named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >' to 'uint8_t &' (aka 'unsigned char &') for 2nd argument
byte_vector_view& operator>>(byte_vector_view&bvv, uint8_t&ui8){
^
itm.cpp:151:19: note: candidate function not viable: no known conversion from 'named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >' to 'std::__1::string &' (aka 'basic_string<char, char_traits<char>, allocator<char> > &') for 2nd argument
byte_vector_view& operator>>(byte_vector_view&bvv, string&str){
^
itm.cpp:156:19: note: candidate function not viable: no known conversion from 'named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >' to 'named_value<std::__1::string> &' (aka 'named_value<basic_string<char, char_traits<char>, allocator<char> > > &') for 2nd argument
byte_vector_view& operator>>(byte_vector_view&bvv, named_value<string>&av){
^
itm.cpp:161:6: note: candidate template ignored: requirement 'is_integral_v<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > || is_same_v<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >, ringnet::proto::opcode> || is_same_v<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >, command_address>' was not satisfied [with T = std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >]
auto operator>>(byte_vector_view&bvv, named_value<T>&av) ->enable_if_t<is_integral_v<T> || is_same_v<T,opcode> || is_same_v<T,command_address>, byte_vector_view&>{
^
itm.cpp:181:6: note: candidate template ignored: requirement 'is_tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' was not satisfied [with TupleT = named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >]
auto operator>>(byte_vector_view&bvv, TupleT&tu) ->enable_if_t<is_tuple<TupleT>,byte_vector_view&> {
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:521:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:570:1: note: candidate template ignored: could not match 'basic_istream<char, type-parameter-0-0>' against 'byte_vector_view'
operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:578:1: note: candidate template ignored: could not match 'basic_istream<char, type-parameter-0-0>' against 'byte_vector_view'
operator>>(basic_istream<char, _Traits>& __is, signed char* __s)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:585:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:613:1: note: candidate template ignored: could not match 'basic_istream<char, type-parameter-0-0>' against 'byte_vector_view'
operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:621:1: note: candidate template ignored: could not match 'basic_istream<char, type-parameter-0-0>' against 'byte_vector_view'
operator>>(basic_istream<char, _Traits>& __is, signed char& __c)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:1219:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp&& __x)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:1287:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is,
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/istream:1419:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iomanip:302:1: note: candidate template ignored: could not match 'basic_istream<type-parameter-0-0, type-parameter-0-1>' against 'byte_vector_view'
operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)

很难阅读和理解问题究竟在哪里,因此很难解决问题

需要更清楚的解释

我找到了一种抑制多余输出的直接方法:
template<typename UnknownType>
auto operator>>(byte_vector_view&bvv, UnknownType&&) ->byte_vector_view& {
static_assert(false);
return bvv;
}

使用它编译器会给出一个安静的简短错误消息,太短了:
itm.cpp:192:5: error: static_assert failed
static_assert(false);
^ ~~~~~
1 error generated.

我想看看错误出现在哪个函数中,并跟踪编译器是如何出现的:
template<typename UnknownType>
auto operator>>(byte_vector_view&bvv, UnknownType&&) ->byte_vector_view& {
static_assert(false&&__PRETTY_FUNCTION__);
return bvv;
}
itm.cpp:192:5: error: static_assert failed
static_assert(false && __PRETTY_FUNCTION__);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
itm.cpp:184:18: note: in instantiation of function template specialization 'operator>><named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > &>' requested here
((bvv>>elements),...);
^
itm.cpp:461:20: note: in instantiation of function template specialization 'operator>><std::__1::tuple<named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > > >' requested here
bvv>>tvs;
^
itm.cpp:427:12: note: in instantiation of member function 'make_converter(bool, std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> > &&, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > &&)::Ret::from_byte_vector_view' requested here
struct Ret:converter_virtual{
^
itm.cpp:537:12: note: in instantiation of function template specialization 'make_converter<std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> >, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
return make_converter(false,forward<Prefix>(prefix),forward<Ts>(values)...);
^
itm.cpp:931:18: note: in instantiation of function template specialization 'make_converter<std::__1::tuple<named_value<ringnet::proto::opcode>, named_value<command_address> >, named_value<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > > >' requested here
/*response*/make_converter(
^
1 error generated.

这几乎看起来像我想要的但是 Clang++ 没有扩展 __PRETTY_FUNCTION__static_assert .

有没有办法把扩展 UnknownTypestatic_assert的消息?

我也试过
static_assert(false,__PRETTY_FUNCTION__);

但它给
error: expected string literal for diagnostic message in static_assert

最佳答案

#include <type_traits>

template <typename UnknownType>
struct Type_Not_Supported : std::false_type {};

template <typename UnknownType>
auto operator>>(byte_vector_view& bvv, UnknownType&&) -> byte_vector_view& {
static_assert(Type_Not_Supported<UnknownType>{});
return bvv;
}

在 Clang 中,这会产生:

prog.cc:10:5: 错误:由于要求“Type_Not_Supported {}”,static_assert 失败
static_assert(Type_Not_Supported {});
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:17:9: 注意:在函数模板特化的实例化 'operator>> ' 请求这里
bvv >> 1;
^
产生了 1 个错误。

在海湾合作委员会中:

prog.cc:在“byte_vector_view& operator>>(byte_vector_view&, UnknownType&&) [with UnknownType = int]”的实例化中:
prog.cc:17:12:从这里需要
prog.cc:10:19: 错误:静态断言失败
10 | static_assert(Type_Not_Supported {});
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

DEMO

关于c++ - 停止编译器建议重载模板的候选,在 static_assert 中扩展 PRETTY_FUNCTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62329960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com