繁体   English   中英

't' 声明中有两种或多种数据类型

[英]two or more data types in declaration of ‘t’

我正在尝试编写自己的编码模板进行编程。 所以基本上我正在尝试编写内联或预处理器来获取编码问题中的测试用例数量。

所以一般我喜欢这样-

int t;
cin>>t;
while(t--){
//code
}

我在这里尝试了两件事

#define test int t; cin>>t; while(t--)
inline void test(){int t; cin>>t; while(t--)}

对于第一种情况,错误是 - 't' 声明中有两种或多种数据类型。 对于第二种情况,错误是 - error: expected primary-expression before '}' token

我究竟做错了什么? 请建议。 PS 我是 C++ 语言的新手

代码可以放入一个函数中:

 void func(int i)
 {.../*code*/... }

然后模板函数接受这个函数,所以它看起来像:

 void template_func(void (*f)(int) )
 {
     int t;
     cin>>t;
     while(t--)
         f(t) ;
  }

这将被称为:

template_func(func) ;

另一种方法是使用template

template<typename Callable>
void template_func(Callable f )
{
    int t;
    cin>>t;
    while(t--)
        f(t) ;
 }

调用它:

template_func(func);

template_func使用 C++17 , std::is_invocable可用于验证传递的函数是否可调用:

static_assert( std::is_invocable_v< decltype(f), int>) ;

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM