2014-12-22 40 views
0

這是function pointers using '<' as an operator的後續問題,它仍然沒有答案。標準操作符的函數指針

這是非常相似的答案代碼我發現這裏:Is it possible to get the function pointer of a built-in standard operator?

問:在上面的鏈接+用作std::operator+所以我爲什麼不能使用std::operator<

目的是不以較少的參數使用重載的功能可按取而代之的東西從std::library

template <typename T> 

bool compare (T a,T b,bool (*compareFunction) (T ,T)) 
{ 
    return compareFunction (a,b); 
} 

template <typename T> 
bool compare (T a,T b) 
{ 
    return a<b; 
} 

bool m (int a ,int b) 
{ 
    return a<b; 
} 
int main() 
{ 
    int a=5,b=6; 

    /* 
    * This Works 
    */ 

    if (compare<int>(a,b,m)) 
     cout<<"Sucess"; 
    else   
     cout<<"Post this in stack overflow "; 

    /* 
    * This also Works 
    */ 

    if (compare<int>(a,b)) 
     cout<<"Sucess"; 
    else 
     cout<<"Post this in stack overflow "; 

    /* 
    * Adding this gives compile error. 
    */ 

    if (compare<int> (a,b,&std::operator<)) 
     cout<<"sucess"; 
    else 
     cout<<"Post in Stackoverflow "; 

return 0; 
} 
+0

誰標記它重複din't甚至讀我的完整問題....我已經看到了你給我的鏈接..事實上,我已經在我的問題本身中提到它...但東西不是工作夥計... – PRP

+0

從第二個鏈接:'內置操作符(內置類型)不是真正的操作員功能。所以你不能有指向它們的函數指針。' –

+1

@PRP _「目標是不使用具有較少參數的重載函數,而是用std :: library中的某些東西替換」_有很好的參考用法來自(現在未鎖定)僞裝中提到的C++標準庫。看起來你正在尋找['std :: less()'](http://en.cppreference.com/w/cpp/utility/functional/less) –

回答

2

你必須要內置運營商和重載運營商之間的差異進行更換。內置運算符是隻具有基本類型(整數,布爾值,浮點數,指針)作爲操作數的運算符。

例如爲:

int a = 9, b = 7; 
a + b; <-- built-in plus operator 
6 + 5; <-- built-in plus operator 

C++給出定義用戶定義類型(即類)運營商的可能性。 std::string是一個類(由庫定義,但沒關係)。因此,如果我們寫:

std::string a = "asdf", b = "qwert"; 
a + b; <-- this is not built-in operator. 

爲了上述編譯必須有,關於字符串類型的兩個對象運行的外加運營商的聲明和定義。恰巧標準庫定義了這樣的運算符:std::operator+(std::basic_string)。所以這個:

std::string a = "asdf", b = "qwert"; 
a + b; 

其實是這樣的:

std::string a = "asdf", b = "qwert"; 
std::operator+(a, b); <-- call to overloaded operator (see this as a function call) 

內置運營商不是函數。重載的運算符就像函數一樣。
內置運算符只有只有基本類型作爲操作數,它們的功能由標準定義(您不能重新定義int + int所做的)。
重載運算符必須至少有一個用戶定義類型的操作數。


讓我們得到一些實際的例子:

template <class T, class Compare> 
bool compare (T a, T b, Compare comp) { 
    return comp(a,b); 
} 

我已經更換了你的函數指針與模板參數。所以這可以是一個函數指針,函數引用或函子。

你有什麼選擇可以稱此爲Tint?那麼你當然不能有一個運營商通過comp。所以你必須有一個函數或函子(見下面的鏈接)。

// function 
bool intCompareLess(int a, int b) { 
    return a < b; 
} 

compare(3, 5, intCompareLess); 


// functor 
class IntComparatorLess { 
public: 
    // this is a function call operator overload 
    bool operator()(int a, int b) { 
     return a < b; 
    } 
}; 

compare(3, 5, IntComparatorLess()); 

// or don't reinvent the wheel: 
compare(3, 5, std::less<int>()); // std::less is somehow similar with IntComparatorLess 

什麼如果調用T比較爲用戶定義類型

class MyClass { 
public: 
    int x, y; 
}; 

現在你有一個額外的選項:定義一​​個運營商和它傳遞的參數:

bool operator<(MyClass c1, MyClass c2) { 
    if (c1.x == c2.x) 
    return c1.y < c2.y; 
    return c1.x < c1.x 
} 

MyClass a, b; 
compare(a, b, (bool (*)(MyClass, MyClass))operator<); 

有用:C++ Functors - and their uses

+0

所以你在說什麼是不存在函數'bool operator <( int a,int b)'在標準庫中 – PRP

+0

不存在。這是不允許的語言。 – bolov

+0

那麼什麼是@πάνταῥεῖ提及廣告std :: less()...?在他的評論 – PRP