現在的簽名是
template<class TypeData,typename TypeFunc>
bool isPrime(const TypeData& n,TypeFunc fSqrt,bool debug = false)
這與
std::cout<<(isPrime(n,fSqrt)?"Positive":"Negative")<<'\n';
但是,我的意圖是這樣的
template<class TypeData,typename TypeFunc>
bool isPrime(const TypeData& n,TypeFunc fSqrt = nullptr,bool debug = false)
或者
template<class TypeData,typename TypeFunc>
bool isPrime(const TypeData& n,TypeFunc fSqrt = NULL,bool debug = false)
被稱為
std::cout<<(isPrime(n)?"Positive":"Negative")<<'\n';
由于函式內部存在靜態變數,因此無法多載。
只有不同的class TypeData應該為此函式模板提供不同的模板函式。
請用正確的語法幫助我。
如果 C 不支持這一點,請建議我使用其他方法。
謝謝你。
編譯錯誤
為了 TypeFunc fSqrt = nullptr
main.cpp:90:23: error: no matching function for call to ‘isPrime(int&)’
std::cout<<(isPrime(n)?"Positive":"Negative")<<'\n';
^
main.cpp:9:49: note: candidate: template bool isPrime(const TypeDate&, TypeFunc, bool)
template<class TypeDate,typename TypeFunc> bool isPrime(const TypeDate& n,TypeFunc fSqrt = nullptr,bool debug = false) {
^~~~~~~
main.cpp:9:49: note: template argument deduction/substitution failed:
main.cpp:90:23: note: couldn't deduce template parameter ‘TypeFunc’
std::cout<<(isPrime(n)?"Positive":"Negative")<<'\n';
^
為了 TypeFunc fSqrt = NULL
main.cpp:90:23: error: no matching function for call to ‘isPrime(int&)’
std::cout<<(isPrime(n)?"Positive":"Negative")<<'\n';
^
main.cpp:9:49: note: candidate: template bool isPrime(const TypeDate&, TypeFunc, bool)
template<class TypeDate,typename TypeFunc> bool isPrime(const TypeDate& n,TypeFunc fSqrt = NULL,bool debug = false) {
^~~~~~~
main.cpp:9:49: note: template argument deduction/substitution failed:
main.cpp:90:23: note: couldn't deduce template parameter ‘TypeFunc’
std::cout<<(isPrime(n)?"Positive":"Negative")<<'\n';
^
它們基本相同。
uj5u.com熱心網友回復:
您可以將其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 {
// ...
}
}
演示。
uj5u.com熱心網友回復:
您可以使用使用std::function正確原型初始化的默認值:
template<class TypeData>
bool isPrime(const TypeData& n,std::function<const decltype(n)&(const decltype(n)&)> fSqrt={},bool debug = false)
然后,您可以簡單地檢查該功能是否有效或默認功能 if (fSqrt)
完整示例:https : //godbolt.org/z/zfMazebso
原型必須僅依賴于 的資料型別n,否則可能沒有默認引數,例如,編譯器無法從無中推匯出型別。
uj5u.com熱心網友回復:
多載實際上是一種選擇,您可以讓一個多載呼叫另一個:
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
}
}
此解決方案會立即選擇一個合適的平方根函式,如果函式引數默認為 ,您無論如何都必須解決這個問題nullptr。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/330033.html
下一篇:去指標解參考
