以下 C/C++ 代码:
long long foo = -9223372036854775808LL; // -2^63
Run Code Online (Sandbox Code Playgroud)
编译(g++)并带有警告
整数常量太大,以至于它是无符号的。
clang++ 给出了类似的警告。
感谢此错误报告:https://gcc.gnu.org/bugzilla/show_bug.cgi ?id= 52661。我现在明白为什么 GCC 发出这个警告了。不幸的是,对错误报告的回应并没有很好地解释这种行为的原因。
问题:
我想要如下设置:
template <typename T> class a {};
class b : public a<int> {};
template <typename T>
void do_foo(std::unique_ptr<a<T>> foo)
{
// Do something with foo
}
int main()
{
do_foo(std::make_unique<b>());
}
Run Code Online (Sandbox Code Playgroud)
这无法通过注释说template argument deduction/substitution failed和进行编译mismatched types 'a<T>' and 'b'。这是不言自明的。我可以通过编写 来帮助编译器do_foo<int>(std::make_unique<b>());,但是我通过编写int两次来重复自己。
在这种情况下,有没有办法让编译器推断出模板参数?你怎么称呼这种行为?我尝试搜索诸如“继承类型的模板类型推导”、“多态模板推导”等内容。