gpt4 book ai didi

c++ - 意外的模板扣除

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:19:31 25 4
gpt4 key购买 nike

template< typename T >
void addVarCB(const std::string &name,
TwSetVarCallback setCallback, TwGetVarCallback getCallback,
void * clientData, const std::string &def = "" );

template< class C, typename T >
void addVarCB(const std::string &name,
C * _this, T(C::*getter)(void), const std::string &def = "");

以下代码将在运行时编译并崩溃:

bar_->addVarCB<MyClass, unsigned>("foo", this, &MyClass::MyClassFn, nullptr);

我实际上希望它根本无法编译,因为没有函数将其参数作为参数! (请注意,“MyClass,未签名”是不必要的,但只是为了清楚...)

最佳答案

遗憾的是,std::string 可以从nullptr 构造,参见here ,特别是 (5):

basic_string( const CharT* s, // <== 'nullptr' matches here
const Allocator& alloc = Allocator() );

注意:

5) Constructs the string with the contents of null-terminated character string pointed to by s. The length of the string is determined by the first null character. s must not be a NULL pointer.

关于c++ - 意外的模板扣除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12483234/

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