Taking address of operator functions

Why can't I take the address of operators for primitives?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
 
int main()
{
    {
        std::string (&plus)(std::string const&, std::string const&) = &std::operator+;
        std::string a ("Hello, "), b("World!");
        std::cout << plus(a, b) << std::endl;
    }
    
    {
        int (&plus)(int const&, int const&) = &operator+; //error
        int a (3), b(4);
        std::cout << plus(a, b) << std::endl;
    } 
}
http://ideone.com/vAWef8

I'm using this functionality in a templated class, do I really have to specialize for primitives or use std::enable_if?
Last edited on
closed account (zb0S216C)
Because the operator must have either a class, an enumerator list object, or a "this" pointer as a parameter. On line 7, you're using the "operator + ( )" member-function of "std::string" which has a "this" pointer and a class ["std::string"] as a parameter.

Because intrinsic types such as "int" are not classes, they don't have a "this" pointer.

Wazzak
Last edited on
Actually operator+ is one of the operators that may be declared in the global scope. If it were a member of std::string I would have to write std::string::operator+ and I would have to use reference-to-member-function syntax.
Last edited on
Topic archived. No new replies allowed.