c++ operator must be non static member function [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does “operator = must be a non-static member” mean? (C++)
I'm trying to write an operator= method as a non member, with 2 arguments like this:
template<class T>
T operator=(T & t, const myclass<T>& m)
{
t = m.val;
return t;
}
But I get the error that operator= must be a nonstatic member. Is there a compiler flag or some way to trick the compiler to let me run this?
Thanks

No there is not, this is mandated by the standard, paragraph 13.5.3.1:
An assignment operator shall be
implemented by a non-static member
function with exactly one parameter.

There isn't, assignment operators need to be declared as members (The rationale is, iirc, to keep you from overriding assignment for primitive or library types).

Related

std::apply to constructor in a templated function [duplicate]

This question already has answers here:
Constructor arguments from tuple
(6 answers)
Closed 3 years ago.
I am writing generic code, and I need to call the constructor of a generic template parameter T with a generic variadic tuple of arguments:
T& init_and_return(ArgsTuple& args)
{
m_data = std::apply(&T::T, args); // here compiler complains
return m_data;
}
In my main, T will be a type called A.
Compiler is saying "no member named T in A".
How can I refer to the constructor of T in a generic way?
The constructor is not a function or a method like other methods are -- it is special, and you cannot take its address. Personally I think it should be possible, but it isn't.
The C++ standard has make from tuple, which does what you want.
m_data = std::make_from_tuple<T>(args);

Are temporary C++ objects lvalues? [duplicate]

This question already has answers here:
Method call acting unexpectedly like an l-value
(4 answers)
Closed 4 years ago.
I'm puzzling over the following code, which (surprisingly, to me) compiles:
class A {
int a=0;
};
A returnsA(void)
{
static A myA;
return myA;
}
void works(void)
{
A anotherA;
returnsA() = anotherA;
}
I can't find anything in the standard or on the web which suggests that it shouldn't compile. It just seems very weird, to me.
I guess returnsA() is returning an object (a copy of myA), so we invoke the default copy assignment operator on it, anotherA gets copy-assigned to the returned object, which then goes out of scope and is destroyed.
I was expecting behavior more like this, which doesn't compile:
int returnsint(void)
{
static int i=0;
return i;
}
void doesntwork(void)
{
int anotherint=0;
returnsint() = anotherint;
}
Can anybody enlighten me further about this behavior?
Objects are not lvalues or rvalues. The words lvalue and rvalue are expression categories. They categorize expressions, not objects.
For the line:
returnsA() = anotherA;
since the operands of = are class types, an overloaded operator function is searched for. operator= is special in that if the user does not explicitly overload it, there is a default overload generated. That overload is a member function with signature:
A& A::operator=(A const&);
and the call returnsA() = anotherA; is transformed to returnsA().operator=(anotherA);, this is the basic mechanics of member operator overloading.
The expression returnsA() has category prvalue. However it is perfectly fine to call member functions on prvalue expressions, so there is no problem here.
If you want to dissuade your class from accepting this sort of assignment, see here. And for why the default assignment operator doesn't work that way, see here.

Overloading type cast operator, to cast to a pointer to function [duplicate]

This question already has answers here:
C++ Conversion operator for converting to function pointer
(6 answers)
Closed 8 years ago.
I'm having some difficulty, overloading the cast to pointer to function operator of a class. In code, what I want is this:
typedef int(*funcptrtype)(int);
struct castable {
operator funcptrtype() {return NULL;}
};
but I want to be able to do it without using the typedef. If you're curious, I need this, because pre-c++11 template aliases aren't available (so the typedef trick is not an option in templated contexts...)
I would normally expect this to work:
operator int(*)(int)() {return NULL;}
But it doesn't. The compiler (g++ 4.6.1) says:
error: ‘<invalid operator>’ declared as function returning a function
This works:
int (* operator()())(int){return 0;}
But you're actually overloading the operator() to return a function pointer :)
The standard says:
The conversion-type-id shall not represent a function type nor an
array type
But it doesn't say function pointer type (The first code snipplet works anyway...).
Does anyone know the right syntax w/o typedef?
The grammar doesn't allow this: the type in a conversion operator declaration is a type-specifier, not a type-id. You have to use a typedef or alias; in a template context, use the usual replacement:
template<typename T>
struct something {
typedef T (*type)(int);
};

Why const object must have a user-provided constructor? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
declaring a const instance of a class
Why does C++ require a user-provided default constructor to default-construct a const object?
My program like this:
class c
{
};
int main()
{
const c a;
return 0;
}
when I compile it using g++, it prompt:
main.cpp:10:7: note: ‘const class c’ has no user-provided default constructor
Why, this is just an empty class and do not do anything, why I have to provide a user-provided constructor.
Because the language rules say so.
The constant must have its value set in the definition, as it cannot be assigned a value later. If you don't explicitly provide a value, the type must have a default constructor.
You don't have to. It's just a note, not even a warning. The rationale is that the class can't do anything useful, which is rarely intended. GCC is just checking to see if you overlooked something.

C++ What is the purpose of casting to void? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
casting unused return values to void
I read some source code, and in it many virtual functions in the interface classes are declared and default-implemented as such:
virtual bool FunctionName(TypeName* pointer)
{
(void)pointer;
return true;
}
May I ask what is the purpose of casting the pointer to void in the default implementation?
Multiple purposes depending on what you cast
Marking your intention to the compiler that an expression that is entirely a no-op is intended as written (for inhibiting warnings, for example)
Marking your intention to to the compiler and programmer that the result of something is ignored (the result of a function call, for example)
In a function template, if a return type is given by a template parameter type T, and you return the result of some function call that could be different from T in some situation. An explicit cast to T could, in the void case, prevent a compile time error:
int f() { return 0; } void g() { return (void)f(); }
Inhibiting the compiler to choose a comma operator overload ((void)a, b will never invoke an overloaded comma operator function).
Note that the Standard guarantees that there will never be an operator void() called if you cast a class object to void (some GCC versions ignore that rule, though).
In this case it's just to avoid compiler's warning about unused parameter.