简体   繁体   English

模板 arguments 中的括号,例如 std::function<int(int, float)></int(int,>

[英]Parentheses inside template arguments e.g. std::function<int(int, float)>

I was reading about std::function in part 3 this (long) answer on callbacks in C++ https://stackoverflow.com/a/28689902/3832877 and it demonstrates the use template arguments which have additional types in parentheses. I was reading about std::function in part 3 this (long) answer on callbacks in C++ https://stackoverflow.com/a/28689902/3832877 and it demonstrates the use template arguments which have additional types in parentheses. Examples of what I mean:我的意思的例子:

std::function<int(int, float)> foo; //for a function returning int with one int and one float argument
std::function<int(C const &, int)> moo; //from the above thread, for member function of class C taking one int argument and returning int

I understand the usage in std::function to define the function signature, but I don't understand how the compiler is parsing these template arguments.我了解std::function中定义 function 签名的用法,但我不明白编译器如何解析这些模板 arguments。 What do the types in the parentheses mean to the compiler?括号中的类型对编译器意味着什么? Are there any other uses for this syntax or was it created specifically for std::function and related STL classes?这种语法还有其他用途,还是专门为std::function和相关的 STL 类创建的? Can I write my own classes that use this syntax?我可以编写自己的使用这种语法的类吗?

Those are types of functions.这些是函数的类型。 int(int,int) is the type of a function that takes two int parameters and returns an int . int(int,int)是 function 的类型,它接受两个int参数并返回一个int

For demonstration, consider this example:为了演示,请考虑以下示例:

#include <type_traits>
#include <iostream>

int foo(int,int){ return 42;}

int main(){
    std::cout << std::is_same< decltype(foo), int(int,int)>::value;
}

It compares the type of foo with the type int(int,int) , and indeed the output is 1 .它将foo的类型与int(int,int)类型进行比较,实际上 output 是1

See also here: https://en.cppreference.com/w/cpp/language/function另请参阅: https://en.cppreference.com/w/cpp/language/function

The type of the function being declared is composed from the return type (provided by the decl-specifier-seq of the declaration syntax) and the function declarator被声明的 function 的类型由返回类型(由声明语法的 decl-specifier-seq 提供)和 function 声明符组成

noptr-declarator ( parameter-list ) cv(optional) ref(optional) except(optional) attr(optional) (1) noptr-declarator ( parameter-list ) cv(optional) ref(optional) except(optional) attr(optional) -> trailing (2) (since C++11)

Can I write my own classes that use this syntax?我可以编写自己的使用这种语法的类吗?

Yes you can.是的你可以。 In a nutshell, int(int,int) is just a type like others:简而言之, int(int,int)和其他类型一样:

#include <iostream>

template <typename T>
void foo(T t){
    t(42);
}

void bar(int x) { std::cout << x; }

int main() {
   foo< void(int) >(bar);
   // ... or ...
   using fun_type = void(int);
   foo< fun_type >(bar);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 标准中的哪里是模板参数的语法,例如,`std::function<int(char)> `定义?</int(char)> - Where in the standard is the grammar for the template argument in, e.g., `std::function<int(char)>` defined? 如何从模板中获取容器的类型(例如 int、double、...)? C++ - How to get the type of a container (e.g. int, double,…) from a template? C++ 将 lambda(int)-&gt;int 转换为 std::function<int(float)></int(float)> - Converting lambda(int)->int to std::function<int(float)> 省略数据类型(例如“ unsigned”而不是“ unsigned int”) - Omitting the datatype (e.g. “unsigned” instead of “unsigned int”) 为什么成员函数需要'&'(例如在std :: bind中)? - Why does a member function needs '&' (e.g. in std::bind)? 比较函数放在哪里(例如std :: sort)? - Where to put comparison function for use with (e.g.) std::sort? 格式化字符串,带有可变大小的参数向量(例如,将参数向量传递给std :: snprintf) - format string with a variable size vector of arguments (e.g. pass vector of arguments to std::snprintf) 没有使用模板函数从`int`自动转换为`float` - No Automatic Cast from `int` to `float` with Template Function 模板函数roundTo int,float - &gt; truncation - template function roundTo int, float -> truncation 与int和std :: string相同的Template函数 - same Template function for int and std::string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM