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);
Related
This question already has answers here:
move semantics/behaviors in std::bind and std::thread
(1 answer)
Where in the source does gcc's std::bind copy arguments into a data structure?
(2 answers)
Closed 3 months ago.
There is something I miss about std::bind and std::thread. Internally, they both forward the rvalue reference template argument (perfect forwarding). But it is known that it copies the object passed to it, instead we wrapp the lvalue references in std::ref(). If you pass a lvalue like the following:
template <typename T>
void fct(T&& x)
{
f(std::forward(x));
}
...
int r{9};
fct(r);
In this case, r is perfectly forwarded as an lvalue.
So, where does the copy occurs, since in the source code, they always use std::forward?
I understand the reason behind the copying step, but how is it really implemented?
Also, this could help me to understand the use of std::ref in depth.
I thank you in advance for your help.
This question already has an answer here:
What is the point of using delete on a non-member function?
(1 answer)
Closed 4 years ago.
This is about non-member functions. I do understand this as an implementation. But I have a bit of puzzlement with the logic behind?
// why this?
void do_not_use_this_ever ( void ) = delete ;
If I do not want a function to be used, why declare it and then delete it? Why not just:
// why not this?
// void do_not_use_this_ever ( void ) = delete ;
If = delete declares an intent, just a comment like above declares the same intent.
Can anyone think of a use-case where declaring a non-member function as deleted is better then not have it at all?
Update
Already answered here . Although. Both answers use std::cref as an example. As #geza said in the comment to his answer, it would be rather beneficial to discuss other use cases as well.
Deleting a non-member function can be useful to disable a function with certain parameters. For example, here is std::cref:
template< class T >
std::reference_wrapper<const T> cref( const T& t ) noexcept;
template <class T>
void cref(const T&&) = delete;
cref is used to convert an object reference to reference_wrapper. This can be used for example with std::bind: std::bind parameters are copied into the resulting object. But with cref, it becomes just a reference. So, cref must not be used with temporary parameters.
If the second overload wasn't deleted, then for example, cref(2) would be a valid expression (as a temporary can be bound to a const reference). This is a problem, as cref would return a reference to an object which will be destroyed. To disallow this, we need to delete functions where cref is passed a temporary, and this is what the second deleted overload does.
This question already has answers here:
A confusing detail about the Most Vexing Parse
(4 answers)
Closed 3 years ago.
Taken directly from http://herbsutter.com/2013/05/09/gotw-1-solution/
While widget w(); is clear for me, I have no idea how can the below code be a function declaration?
// same problem (gadget and doodad are types)
//
widget w( gadget(), doodad() ); // pitfall: not a variable declaration
How is this possible?
In a function declaration, arguments of type array decay into pointers to the first element, arguments of type function decay into a function pointer, so the signature would be:
widget w( gadget(*)(), doodad(*)() );
That is, a function that takes as the first argument a pointer to a function taking no arguments and returning gadget, that takes as second argument a pointer to a function taking no arguments and returning a doodad and that the function itself returns a widget
There are even more interesting or confusing cases, like:
// assume 'x' is a variable defined somewhere:
widget w(gadget(x));
How could that be interpreted as a function declaration? I mean, x is a variable, right? Well, when declaring a variable you can add extra parenthesis, so gadget x; and gadget (x); both declare the same variable x. The same applies to function arguments so the code above looks like a declaration of a function that takes a first argument named x of type gadget and returns a widget...
It's function that gets two functions, that returns gadget and doodad and either of them gets no arguments.
Example that compiles fine.
#include <iostream>
class widget{};
class gadget{};
class doodad{};
gadget a(){}
doodad b() {};
widget w( gadget(), doodad() ){
}
int main() {
w(a,b);
return 0;
}
http://ideone.com/YjZK9Y
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).
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.