Sup*_*eto 10 c++ templates pointers function-pointers default-arguments
目前的签名是
\ntemplate<class TypeData,typename TypeFunc>\nbool isPrime(const TypeData& n,TypeFunc fSqrt,bool debug = false)\nRun Code Online (Sandbox Code Playgroud)\n这与
\nstd::cout<<(isPrime(n,fSqrt)?"Positive":"Negative")<<'\\n';\nRun Code Online (Sandbox Code Playgroud)\n但是,我的意图是这样的
\ntemplate<class TypeData,typename TypeFunc>\nbool isPrime(const TypeData& n,TypeFunc fSqrt = nullptr,bool debug = false)\nRun Code Online (Sandbox Code Playgroud)\n或者
\ntemplate<class TypeData,typename TypeFunc>\nbool isPrime(const TypeData& n,TypeFunc fSqrt = NULL,bool debug = false)\nRun Code Online (Sandbox Code Playgroud)\n被称为
\nstd::cout<<(isPrime(n)?"Positive":"Negative")<<'\\n';\nRun Code Online (Sandbox Code Playgroud)\n由于函数内部有静态变量,因此不可能重载。
\n只有不同才应该为此function-templateclass TypeData提供不同的template-functions。
请帮助我使用正确的语法。如果 C++ 不支持这一点,我可以使用什么替代方法?
\n编译错误
\n为了TypeFunc fSqrt = nullptr
main.cpp:90:23: error: no matching function for call to \xe2\x80\x98isPrime(int&)\xe2\x80\x99\n std::cout<<(isPrime(n)?"Positive":"Negative")<<'\\n';\n ^\nmain.cpp:9:49: note: candidate: template bool isPrime(const TypeDate&, TypeFunc, bool)\n template<class TypeDate,typename TypeFunc> bool isPrime(const TypeDate& n,TypeFunc fSqrt = nullptr,bool debug = false) {\n ^~~~~~~\nmain.cpp:9:49: note: template argument deduction/substitution failed:\nmain.cpp:90:23: note: couldn't deduce template parameter \xe2\x80\x98TypeFunc\xe2\x80\x99\n std::cout<<(isPrime(n)?"Positive":"Negative")<<'\\n';\n ^\nRun Code Online (Sandbox Code Playgroud)\n为了TypeFunc fSqrt = NULL
main.cpp:90:23: error: no matching function for call to \xe2\x80\x98isPrime(int&)\xe2\x80\x99\n std::cout<<(isPrime(n)?"Positive":"Negative")<<'\\n';\n ^\nmain.cpp:9:49: note: candidate: template bool isPrime(const TypeDate&, TypeFunc, bool)\n template<class TypeDate,typename TypeFunc> bool isPrime(const TypeDate& n,TypeFunc fSqrt = NULL,bool debug = false) {\n ^~~~~~~\nmain.cpp:9:49: note: template argument deduction/substitution failed:\nmain.cpp:90:23: note: couldn't deduce template parameter \xe2\x80\x98TypeFunc\xe2\x80\x99\n std::cout<<(isPrime(n)?"Positive":"Negative")<<'\\n';\n ^\nRun Code Online (Sandbox Code Playgroud)\n它们基本上是相同的。
\n您可以将std::identity其用作默认TypeFunc类型。
#include <functional>
template<class TypeData,typename TypeFunc = std::identity>
bool isPrime(const TypeData& n,TypeFunc fSqrt = {}, bool debug = false) {
if constexpr (std::is_same_v<TypeFunc, std::identity>) return false;
else {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
如果您的编译器不支持 C++20,您可以简单地定义自己的std::identity:
struct Identity {
template<class T>
constexpr decltype(auto) operator()(T&& t) const noexcept {
return std::forward<T>(t);
}
};
Run Code Online (Sandbox Code Playgroud)
重载实际上是一种选择,您可以让一个重载调用另一个重载:
template<class TypeData, typename TypeFunc>
bool isPrime(const TypeData& n, TypeFunc fSqrt, bool debug = false);
template<class TypeData>
bool isPrime(const TypeData& n, bool debug = false)
{
using std::sqrt;
if constexpr (std::is_integral_v<TypeData>)
{
return isPrime(n, static_cast<double(*)(double)>(sqrt), debug);
}
else if constexpr (std::is_floating_point_v<TypeData>)
{
return isPrime(n, static_cast<TypeData(*)(TypeData)>(sqrt), debug);
}
else
{
// this covers e.g. std::complex
return isPrime(n, static_cast<TypeData(*)(TypeData const&)>(sqrt), debug);
// for any other type we assume the overload accepts by
// const reference as well (if there's one at all...)
// if not, we still can fall back to the other overload
}
}
Run Code Online (Sandbox Code Playgroud)
该解决方案立即选择合适的平方根函数,如果函数参数默认为 ,则无论如何您都必须解决这个问题nullptr。
| 归档时间: |
|
| 查看次数: |
628 次 |
| 最近记录: |