Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数模板专门化-错误-ISO C++;禁止申报‘;参数’;没有类型 这里是C++代码编程的第700页代码:FoouZhan&Gielbg.O/P>面向对象的方法_C++_Function_Templates_Generic Programming - Fatal编程技术网

函数模板专门化-错误-ISO C++;禁止申报‘;参数’;没有类型 这里是C++代码编程的第700页代码:FoouZhan&Gielbg.O/P>面向对象的方法

函数模板专门化-错误-ISO C++;禁止申报‘;参数’;没有类型 这里是C++代码编程的第700页代码:FoouZhan&Gielbg.O/P>面向对象的方法,c++,function,templates,generic-programming,C++,Function,Templates,Generic Programming,据我所知,这是为了解释函数模板专门化,我已经阅读并看到很多人说,为了避免这种情况,他们倾向于重载,traits类(不管是什么),我想还有其他方法-所以我不打算花太多时间在上面,但我想学习它。@Roger Pate提供了一个答案,其中有一个很好的链接,我还在吸收它(在下面一个类似问题的链接中): 始终抛出此输出,而不考虑我尝试的修复(见下文) 2122|1|root@sbh~/CODING/cppmodel#Sun 11 08 2019,08:56:33 g++-8-Wall-O-std=c++2

据我所知,这是为了解释
函数模板专门化
,我已经阅读并看到很多人说,为了避免这种情况,他们倾向于
重载
traits类
(不管是什么),我想还有其他方法-所以我不打算花太多时间在上面,但我想学习它。@Roger Pate提供了一个答案,其中有一个很好的链接,我还在吸收它(在下面一个类似问题的链接中):

始终抛出此输出,而不考虑我尝试的修复(见下文)

2122|1|root@sbh~/CODING/cppmodel#Sun 11 08 2019,08:56:33
g++-8-Wall-O-std=c++2a templateSpecialization.cpp-O templateSpecialization.out
错误:ISO C++禁止声明“参数”,没有类型[fPrime]。
常量字符*较小(常量(常量字符*)&第一个,常量(常量字符*)&第二个)
^
templateSpecialization.cpp:21:44:错误:在“first”之前应为“,”或“…”
常量字符*较小(常量(常量字符*)&第一个,常量(常量字符*)&第二个)
^~~~~
templateSpecialization.cpp:21:13:错误:“常量字符*较小(常量int(*)(常量字符*))”的模板id“较小”与任何模板声明都不匹配
常量字符*较小(常量(常量字符*)&第一个,常量(常量字符*)&第二个)
^~~~~~~
templateSpecialization.cpp:8:3:注意:候选者是:'template T较小(const T&,const T&)'
T较小(常数T和第一、常数T和第二)
^~~~~~~
我注意到@jagannath提出的这个问题,这个问题的答案与功能的精神是一样的

template<typename T>
bool Less(T a, T b)
{
    cout << "version 1 ";
    return a < b;
}
// Function templates can't be partially specialized they can overload instead.
template<typename T>
bool Less(T* a, T* b)
{
    cout << "version 2 ";
    return *a < *b;
}

template<>
bool Less<>(const char* lhs, const char* rhs)
{
    cout << "version 3 ";
    return strcmp(lhs, rhs) < 0;
}

int a = 5, b = 6;

cout << Less<int>(a, b) << endl;
cout << Less<int>(&a, &b) << endl;
cout << Less("abc", "def") << endl;
模板
无布尔(T a,T b)
{

请尝试将类型定义为:

模板
常量字符*较小(常量字符*常量和第一个,常量字符*常量和第二个);
或:

您可以从右到左阅读:“引用指向常量字符的常量指针”。无需括号


第二个示例显示了一个更清晰的示例,如果您不想与类型混淆的话。两个函数签名解析为相同的类型。

哦,谢谢-它成功了!我想知道为什么作者的示例不起作用……看起来很奇怪……在第一个示例中,通过放括号,编译器认为您试图使用funct离子类型
()
作为参数。但是,由于
const
不是类型,它告诉参数函数需要返回类型。因此括号具有最佳优先级;)现在我注意到我的第44行和第45行也起作用了。我相信优先级与以下情况类似:如果某个带括号的表达式不明确,它将作为函数声明消除歧义。如果您对模板将如何实例化感到好奇,它通常会有所帮助。nice平台(如超级objdump)不知道它,它使用type_traits是的,它基本上编译并显示你提供给它的任何代码的程序集。该网站也有很多编译器,有时会非常有用。
/***************************************************************
* Template function definition with specialization *
***************************************************************/
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

// Template Function
template <typename T>
T smaller (const T& first, const T& second)
{
  if (first < second)
  {
    return first;
  }
  return second;
}

// Specialization of template function
template <>
const char* smaller (const (const char*) & first, const (const char*) & second)
{
  if (strcmp (first, second ) < 0)
  {
    return first;
  }
  return second;
}

int main ( )
{
  // Calling template with two string objects
  string str1 = "Hello";
  string str2 = "Hi";
  cout << "Smaller (Hello , Hi): " << smaller (str1, str2) << endl;

  //Calling template function with two C-string objects
  const char* s1 = "Bye";
  const char* s2 = "Bye Bye";
  cout << "Smaller (Bye, Bye Bye)" << smaller (s1, s2) << endl;

  return 0;
}
2120|1|root@sbh ~/CODING/CppTemplate # Sun 11 08 2019, 08:53:15 
 /usr/bin/gcc-8 --version
gcc-8 (Debian 8.3.0-19) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
2122|1|root@sbh ~/CODING/CppTemplate # Sun 11 08 2019, 08:56:33 
 g++-8 -Wall -O -std=c++2a templateSpecialization.cpp -o templateSpecialization.out 
templateSpecialization.cpp:21:42: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
 const char* smaller (const (const char*) & first, const (const char*) & second)
                                          ^
templateSpecialization.cpp:21:44: error: expected ‘,’ or ‘...’ before ‘first’
 const char* smaller (const (const char*) & first, const (const char*) & second)
                                            ^~~~~
templateSpecialization.cpp:21:13: error: template-id ‘smaller<>’ for ‘const char* smaller(const int (*)(const char*))’ does not match any template declaration
 const char* smaller (const (const char*) & first, const (const char*) & second)
             ^~~~~~~
templateSpecialization.cpp:8:3: note: candidate is: ‘template<class T> T smaller(const T&, const T&)’
 T smaller(const T& first, const T& second)
   ^~~~~~~
template<typename T>
bool Less(T a, T b)
{
    cout << "version 1 ";
    return a < b;
}
// Function templates can't be partially specialized they can overload instead.
template<typename T>
bool Less(T* a, T* b)
{
    cout << "version 2 ";
    return *a < *b;
}

template<>
bool Less<>(const char* lhs, const char* rhs)
{
    cout << "version 3 ";
    return strcmp(lhs, rhs) < 0;
}

int a = 5, b = 6;

cout << Less<int>(a, b) << endl;
cout << Less<int>(&a, &b) << endl;
cout << Less("abc", "def") << endl;
using cstring_t = const char*;
template <>
const char* smaller (const cstring_t& first, const cstring_t& second);
char const * const &