in Education by
I have a function value(x) which is overloaded for many types such that: double value(double x) { return x; } double value(MyType x) { return x.value(); } SomeContainer value(SomeContainer x) { return x; } SomeContainer value(SomeContainer x) { ... } where MyType is actually a number with a gradient vector with respect to a set of parameters. for use in generic (template) programs. I want to define: Matrix value(Matrix) Matrix value(Matrix) I am using Eigen matrices and this is my current implementation of the first function: template < typename Derived, typename std::enable_if< std::is_floating_point< typename Derived::Scalar >::value, int >::type = 0 > Derived value( const Eigen::MatrixBase< Derived >& matrix ) { return matrix; } The problem is that this seems inefficient unless in possible cases where the compiler can figure out that the result/argument are not being modified and elude the copy. I also cannot return a reference to the argument since it is a local/temporary. Basically what I would like is for value(x) to be compiled as the argument expression itself if the argument is a Matrix of double/float. I don't see how I can achieve this with a function template and a macro would not allow for specialization. What could be done to avoid the copy? EDIT March 22, 2019: If I replace the return type by const Derived & I get the following GCC warning: warning: returning reference to temporary [-Wreturn-local-addr] in the following code: Matrix33 m; m << 1, 2, 3, 4, 5, 6, 7, 8, 9; std::cout << m << std::endl; std::cout << value(m) << std::endl; and the value(m) printout is garbage. Also I am now thinking more and more this would be a bad idea to "return the object itself" because it is going to be used in generic code: auto m = value(Mx) where Mx is a Matrix of X (template parameter) and m is a double matrix. Having different semantics (stealing the object in case X is double and creating a separate object in other cases) could lead to many programming mistakes. Another possibility would be to return proxy objects. The best however would be for the compiler to see when a copy is not needed because nothing is being changed. However this does not seem to be the case: my benchmark to compare Matrix<double,3,3> M = ... norm(M) and Matrix M = ... norm(value(M)) shows that the second is a bit slower in a release (optimised) build. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You need to return matrix.derived() if you want to have a reference to the encapsulated type: template < typename Derived, typename std::enable_if< std::is_floating_point< typename Derived::Scalar >::value, int >::type = 0 > const Derived& value( const Eigen::MatrixBase< Derived >& matrix ) { return matrix.derived(); } Regarding value(Matrix const&), you can implement that in a way that it returns just a view, essentially like this (requires C++14, or some more implementation work): template < typename Derived, typename std::enable_if< std::is_same< typename Derived::Scalar, MyType >::value, int >::type = 0 > auto value( const Eigen::MatrixBase< Derived >& matrix ) { // If `x.value()` returns by value, better use `unaryExpr` instead of `unaryViewExpr`: return matrix.unaryViewExpr([](const MyType& x){ return x.value();} ); } Godbolt-Link: https://godbolt.org/z/qcYKnw No warnings are generated and both foo1 and foo2 just tail-call the norm function. foo3 has some small overhead, which likely would get optimized away if norm was inlined.

Related questions

0 votes
    #include struct A { bool f( int a ) { std::cout...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    // merge.cpp // merges two containers into a third #include #include //for merge() using namespace std; int src1[] = { 2, 3, 4, ... , src1+5, src2, src2+3, dest); for(int j=0; j...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    #include using namespace std; template class test { T y; public: test(T k) : y(k) {} friend int a(T& x); }; template int a(T& x) { cout...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    #include using namespace std; template class test { T y; public: test(T k) : y(k) {} friend int a(T& x); }; template int a(T& x) { cout...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    #include using namespace std; template class test { T y; public: test(T k) : y(k) {} friend int a(T& x); }; template int a(T& x) { cout...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    perimeter of circle in c program without argument with return type Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...) printf(fmt); Is ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I am trying to generate 10,000 unique random integers in the range of 1 to 20,000 to store in a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I am trying to generate 10,000 unique random integers in the range of 1 to 20,000 to store in a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    _________ require you to pass a function whose argument is a vector of parameters. (a) optimize() (b) ... Debugging of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    To get the current date, the _______ function will return a Date object which can be converted to a different ... of R Programming Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    I'm working on a fairly complex project, a custom encryption routine if you will (just for fun) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm working on a fairly complex project, a custom encryption routine if you will (just for fun) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm working on a fairly complex project, a custom encryption routine if you will (just for fun) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm working on a fairly complex project, a custom encryption routine if you will (just for fun) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 18, 2022 in Education by JackTerrance
...