[英]Template type deduction C++
I am getting an error qualified-id in declaration before '<' token
from the following code:我在以下代码
qualified-id in declaration before '<' token
:
// g++ -std=c++20 example.cpp
#include <iostream>
template <typename U = int>
struct Example {
template <typename T>
static void execute() {
std::cout << "Hey" << std::endl;
}
};
int main() {
Example::execute<float>();
}
When I include the type for Example, such as Example<int>::execute<float>()
it compiles successfully.当我包含 Example 的类型时,例如
Example<int>::execute<float>()
它编译成功。 Shouldn't the compiler be able to deduce the type since I specified it as default value?由于我将其指定为默认值,编译器不应该能够推断出类型吗?
Class template argument deduction only applies when creating objects. Class 模板参数推导仅适用于创建对象时。
That is Example e;
即
Example e;
will deduce Example<int> e;
将推导出
Example<int> e;
via the default argument.通过默认参数。
You are not creating an object though, and Example
is not a class.但是,您没有创建 object,并且
Example
不是 class。 You must include a template argument list.您必须包含一个模板参数列表。 In this case, it can be empty though, since the template argument includes a default:
在这种情况下,它可以为空,因为模板参数包含一个默认值:
Example<>::execute<float>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.