C++ static operator overloading
Asked Answered
E

5

8

Is it possible to overload C++ class operators in the static context? e.g.

class Class_1{ ... }
int main()
{

    Class_1[val]...

}
Elkeelkhound answered 7/9, 2009 at 18:55 Comment(3)
Should Class_1[val] yield a type? Like do you expect to do Class_1[val] v; ?Filtration
I was going to have it yield integral values but types are fine.Elkeelkhound
In C++, types cannot be represented in the type system (there's only std::type_id) and I can't think of any operator that can be overloaded statically. I suggest, instead of asking whether your implementation idea is feasible in C++, you present the actual problem this was meant to solve. Someone might be able to suggest a feasible implementation.Assyria
F
14

If you are looking for metaprogramming using the built-in operator: Such a thing isn't possible - the built-in operators operate on runtime values, not on compile time values.

You may use boost::mpl for that, and instead of using the built-in operators, use its templates, like at for op[], plus<a, b> for op+ etc.

int main() {
    using boost::mpl::vector;
    using boost::mpl::at_c;
    using boost::mpl::plus;
    using boost::mpl::int_;

    typedef vector<int, bool, char, float> Class_1;
    typedef vector< int_<1>, int_<2> > Numeric_1;

    at_c<Class_1, 0>::type six = 6;
    typedef plus<at_c<Numeric_1, 0>::type 
                ,at_c<Numeric_1, 1>::type>::type r;
    int i3[r::value] = { 4, 5, 6 };
    return ((i3[0] + i3[1] + i3[2]) * six) == 90;
}
Filtration answered 7/9, 2009 at 19:43 Comment(0)
K
5

I don't believe it's possible, though I could be wrong on this front. I'd like to ask why you'd want to do this though. Rather than performing operations on a class instead of instances, perhaps you just require one instance throughout your application? In this case, you should probably be using the singleton pattern.

Kendall answered 7/9, 2009 at 19:2 Comment(1)
I used to use the singleton pattern like mad, but then stopped because singleton code is such a hassle to write tests for.Elkeelkhound
M
3

If you mean the operator operates on a class, the No. That does not make sense, it is like saying operator + may operator on int or double. Operators are syntactic sugar for functions and they operate on varibles(values) not types.

Managing answered 7/9, 2009 at 19:1 Comment(2)
It makes sense if you have some kind of utility class - with only static members - that loads something to "itself", which then you would like to access in the most simple way - using operator like [].Proclivity
Yep, adding to @jave.web's comment: it certainly can make sense in cases when there's data in the static scope of the class. (The OP also mentions the static scope explicitly.) So, not with simple arithmetics, as in your example, but stuff like [], (), <<, >> etc. might be perfect syntactic sugar for any sort of complex manager classes, too, without forcing a singleton instance.Treat
W
1

No, operators cannot be static members of a class. Use a regular static function instead.

Womankind answered 7/9, 2009 at 19:5 Comment(0)
T
0

As of C++23, it's possible to overload static operator(). As for other operators:

There are other operators that are currently required to be implemented as non-static member functions - all the unary operators, assignment, subscripting, conversion functions, and class member access. We do not believe that being able to declare any of these as static will have as much value, so we are not pursuing those at this time.

A compiler indicates support for this feature with __cpp_static_call_operator.

Taboo answered 11/5, 2023 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.