gpt4 book ai didi

c++ - 函数模板特化 - 错误 - ISO C++ 禁止声明没有类型的 ‘parameter’

转载 作者:行者123 更新时间:2023-11-30 03:14:49 27 4
gpt4 key购买 nike

这是示例代码(第 700 页,来自 Forouzan 和 Gilberg 的 C++ 编程:一种面向对象的方法

据我了解,它是为了解释函数模板特化,而且我已经阅读并看到很多人说要避免这种情况以支持重载 traits class(不管是什么),我想还有其他方法——所以我不打算花太多时间在上面,但我想学习它。 @Roger Pate 通过这个很好的链接提供了一个答案,我仍在吸收这个链接(在下面类似问题的链接中):http://www.gotw.ca/publications/mill17.htm

 1 #include <iostream>
2 #include <string>
3 #include <cstring>
4 using namespace std;
5
6 // Template Function
7 template <typename T>
8 T smaller(const T& first, const T& second)
9 {
10 if (first < second)
11 {
12 return first;
13 }
14 return second;
15 }
16
17 // Specialization of template function (previously defined)
18 // a C-style string is of type: const char*
19 // so replace every "T" with that
20 template <>
21 const char* smaller (const (const char*) & first, const (const char*) & second)
22 //const char* smaller<>(const (const char*)& first, const (const char*)& second)
23 //const char* smaller<const char*>(const (const char*)& first, const (const char*)& second)
24 {
25 if (strcmp (first, second ) < 0)
26 {
27 return first;
28 }
29 return second;
30 }
31
32 int main ( )
33 {
34
35 // Calling template with two string objects
36 string str1 = "Hello";
37 string str2 = "Hi";
38 cout << "Smaller (Hello , Hi): " << smaller (str1, str2) << endl;
39
40 //Calling template function with two C-string objects
41 const char* s1 = "Bye";
42 const char* s2 = "Bye Bye";
43 cout << "Smaller (Bye, Bye Bye)" << smaller (s1, s2) << endl;
44 // cout << "Smaller (Bye, Bye Bye)" << smaller<>(s1, s2) << endl;
45 // cout << "Smaller (Bye, Bye Bye)" << smaller<const char*>(s1, s2) << endl;
46
47 return 0;
48 }
49
50
51 /*
52 The operator < is not defined for a C-style string, meaning we cannot use the template
53 function to find the smaller of the two C-style strings.
54 This operator is defined in the library "string", and so overloads the operator
55 */

相同的代码,没有行号和我失败的尝试:

/***************************************************************
* 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;
}

长话短说,我的编译器gcc-8

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)
^~~~~~~

我注意到这个问题,它的答案具有相同的功能精神,Function template specialization importance and necessity通过 https://stackoverflow.com/users/124797/jagannath @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”替换为“const char*”,例如第 22 和 23 行。

我阅读了一些关于编译器“推导”使用什么的内容,所以我尝试在调用中在函数名称之后添加方括号(第 44 和 45 行)。

我敢肯定这很简单,可能在其他地方也有介绍,但我找不到它。甚至可能是在类模板上下文中的解释,但我刚刚开始学习模板,这是我所了解的(即那本书中的第 700 页)。

连这个问题Function Template Specialization Error似乎有类似的问题 does not match any template declaration 但同样,我觉得我已经遵循了模式,如第 21 行(作者的)以及我的第 22 和 23 行。

附言:我认为这无关紧要,但我的环境可能有点奇怪......

最佳答案

尝试将类型定义为:

template <>
const char* smaller (const char* const &first , const char* const &second);

或者:

using cstring_t = const char*;
template <>
const char* smaller (const cstring_t& first, const cstring_t& second);

对于第一种情况,const char* const &,如果使用“east const”表示法:

char const * const &

您可以从右到左阅读:“对指向常量 char 的常量指针的引用”。不需要括号。

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

关于c++ - 函数模板特化 - 错误 - ISO C++ 禁止声明没有类型的 ‘parameter’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57451280/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com