为什么具有相同名称和参数类型检查的函数不能共存?
我希望下面的 2 个定义能够共存,因为我添加了类型检查代码,但它给出了已经声明的错误。为什么会这样以及需要改变什么?
#include <iostream>
#include <type_traits>
template<class T, class = std::enable_if_t<std::is_integral_v<T>>>
bool is_even(T value)
{
return ((value % 2) == 0);
}
template<class T, class = std::enable_if_t<std::is_floating_point_v<T>>>
bool is_even(T value)
{
std::cout << "\n Floating point version ";
return false;
}
int main()
{
std::cout << "is_even (4) = " << std::boolalpha << is_even(4);
std::cout << "is_even (4.4) = " << std::boolalpha << is_even(4.4);
}
错误如下
Error(s):
462942513/source.cpp:13:6: error: redefinition of ‘template<class T, class> bool is_even(T)’
bool is_even(T value)
^~~~~~~
462942513/source.cpp:7:6: note: ‘template<class T, class> bool is_even(T)’ previously declared here
bool is_even(T value)
^~~~~~~
462942513/source.cpp: In function ‘int main()’:
462942513/source.cpp:22:69: error: no matching function for call to ‘is_even(double)’
std::cout << "is_even (4.4) = " << std::boolalpha << is_even(4.4);
^
462942513/source.cpp:7:6: note: candidate: template<class T, class> bool is_even(T)
bool is_even(T value)
^~~~~~~
462942513/source.cpp:7:6: note: template argument deduction/substitution failed:
即使没有默认参数的语法也不起作用
#include <iostream>
#include <type_traits>
template<class T, std::enable_if_t<std::is_integral_v<T>>>
bool is_even(T value)
{
return ((value % 2) == 0);
}
template<class T, std::enable_if_t<std::is_floating_point_v<T>>>
bool is_even(T value)
{
return false;
}
int main()
{
std::cout << "is_even (4) = " << std::boolalpha << is_even(4) << "\n";
std::cout << "is_even (4.4) = " << std::boolalpha << is_even(4.4) << "\n";
}
I expected the 2 definitions below to co-exist because i am adding a type checking code but it gives error as already declared. Why so and what needs to be changed?
#include <iostream>
#include <type_traits>
template<class T, class = std::enable_if_t<std::is_integral_v<T>>>
bool is_even(T value)
{
return ((value % 2) == 0);
}
template<class T, class = std::enable_if_t<std::is_floating_point_v<T>>>
bool is_even(T value)
{
std::cout << "\n Floating point version ";
return false;
}
int main()
{
std::cout << "is_even (4) = " << std::boolalpha << is_even(4);
std::cout << "is_even (4.4) = " << std::boolalpha << is_even(4.4);
}
Errors as below
Error(s):
462942513/source.cpp:13:6: error: redefinition of ‘template<class T, class> bool is_even(T)’
bool is_even(T value)
^~~~~~~
462942513/source.cpp:7:6: note: ‘template<class T, class> bool is_even(T)’ previously declared here
bool is_even(T value)
^~~~~~~
462942513/source.cpp: In function ‘int main()’:
462942513/source.cpp:22:69: error: no matching function for call to ‘is_even(double)’
std::cout << "is_even (4.4) = " << std::boolalpha << is_even(4.4);
^
462942513/source.cpp:7:6: note: candidate: template<class T, class> bool is_even(T)
bool is_even(T value)
^~~~~~~
462942513/source.cpp:7:6: note: template argument deduction/substitution failed:
Even this syntax without default arguments doesn't work
#include <iostream>
#include <type_traits>
template<class T, std::enable_if_t<std::is_integral_v<T>>>
bool is_even(T value)
{
return ((value % 2) == 0);
}
template<class T, std::enable_if_t<std::is_floating_point_v<T>>>
bool is_even(T value)
{
return false;
}
int main()
{
std::cout << "is_even (4) = " << std::boolalpha << is_even(4) << "\n";
std::cout << "is_even (4.4) = " << std::boolalpha << is_even(4.4) << "\n";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 std::enable_if 的文档:
在给定示例中解决此问题的一种方法是在指定函数模板的返回类型时使用
enable_if_t
表达式,而不是作为默认参数,如下所示:演示
解决方案 2
演示
From std::enable_if 's documentation:
One way to solve this problem in your given example would be to use the
enable_if_t
expression when specifying the return type of the function template instead of as a default argument, as shown below:Demo
Solution 2
Demo