expression-templates

Check for multiple values when using comparison operators

妖精的绣舞 提交于 2020-01-02 01:16:08
问题 I've always been under the impression that for any comparison statement, i.e. X == Y or X != Y is the format, and you chain statements together with && or || . Is there not some way to write X == (Y || Z) instead of X == Y || X == Z ? Edit : Since it has been established that this is not possible to do cleanly, how else could it be done? 回答1: #include <algorithm> #include <array> #include <string> #include <iostream> #include <initializer_list> template<class Type, class Next> bool is_one_of

Expression templates are not being inlined fully

浪子不回头ぞ 提交于 2020-01-01 04:12:10
问题 I have the first version of a math library completed, and for the next step I'd like to turn to expression templates to improve the performance of the code. However, my initial results are different than I expected. I am compiling in MSVC 2010, in vanilla Release mode (and am okay with C++0x). Apologies in advance for the large amount of code I'll be showing you, it's as minimal as I can make it while letting people look at what I'm doing. Profiling framework: #include <algorithm> #include

Expression Template implementation not being optimized

时光毁灭记忆、已成空白 提交于 2019-12-13 20:00:02
问题 I'm trying to understand the concept of expression templates in C++, as such I've cobbled together pieces of example code etc to produce a simple vector and associated expression template infrastructure to support only binary operators (+,-,*). Everything compiles, however I've noticed the performance difference between the standard hand written loop versus the expression template variant is quite large. ET is nearly twice as slow as the hand written. I expected a difference but not that much

How to write a third-party library wrapper class around expression templates

百般思念 提交于 2019-12-12 10:39:49
问题 We are trying to implement a new C++ code in my research group to perform large numerical simulations (finite elements, finite difference methods, topology optimization, etc.) The software will be used by people from academia and industry alike. For the dense linear algebra piece of the software, we want to use either Eigen or Armadillo. We wish to build a wrapper around these packages for two reasons: 1. to expose our own API to the users rather than the third-party API; and 2. in case we

template method matching derived type instead of base

帅比萌擦擦* 提交于 2019-12-12 01:20:06
问题 I have a set of operators that I need to override for expression templating. I would like all derived classes of a base type match to the base type. Other things would then be caught by a generic type. Unfortunately, the generic type grabs the derived types before the base type does. To make things nice and confusing, everything is templated pretty heavily, including some CRTP. Let me try to give a more simple version of the code: // Note: 'R' is used for return type template <typename

cast from Eigen::CwiseBinaryOp to MatrixXd causes segfault

烂漫一生 提交于 2019-12-11 19:40:06
问题 I am writing a library that stores Eigen expression templates as member variables to do the complicated calculations it needs to do. However, it seems like I'm not able to store or return these expression templates unless they are directly converted in MatrixXd or alike. This forces every step to be saved to a temporary, and ruins the efficiency of the whole design. Here's a short example that causes the trouble. Holder just holds an Eigen matrix, and Summer takes two holders and outputs the

Avoiding need for #define with expression templates

核能气质少年 提交于 2019-12-11 07:36:49
问题 With the following code, "hello2" is not displayed as the temporary string created on Line 3 dies before Line 4 is executed. Using a #define as on Line 1 avoids this issue, but is there a way to avoid this issue without using #define? (C++11 code is okay) #include <iostream> #include <string> class C { public: C(const std::string& p_s) : s(p_s) {} const std::string& s; }; int main() { #define x1 C(std::string("hello1")) // Line 1 std::cout << x1.s << std::endl; // Line 2 const C& x2 = C(std:

How to prevent QStringBuilder from outliving the scope it was initialised in

梦想的初衷 提交于 2019-12-11 03:45:31
问题 I have been looking at changing some code to make use of QStringBuilder expression template for its purported performance improvements. Unfortunately this resulted in sections of my code starting to crash in places, an example of which follows: #define QT_USE_QSTRINGBUILDER #include <numeric> #include <vector> #include <QString> #include <QStringBuilder> int main() { std::vector<int> vals = {0, 1, 2, 3, 4}; QString text = "Values: " + QString::number(vals[0]); text = std::accumulate(vals

nested std::forward_as_tuple and segmentation fault

浪子不回头ぞ 提交于 2019-12-11 00:29:35
问题 My actual problem is a lot more complicated and it seems extremely difficult to give a short concrete example here to reproduce it. So I am posting here a different small example that may be relevant, and its discussion may help in the actual problem as well: // A: works fine (prints '2') cout << std::get <0>(std::get <1>( std::forward_as_tuple(3, std::forward_as_tuple(2, 0))) ) << endl; // B: fine in Clang, segmentation fault in GCC with -Os auto x = std::forward_as_tuple(3, std::forward_as

Prevent expression templates binding to rvalue references

你离开我真会死。 提交于 2019-12-09 15:39:31
问题 I understand that doing something like the following: auto&& x = Matrix1() + Matrix2() + Matrix3(); std::cout << x(2,3) << std::endl; Will cause a silent runtime error if the matrix operations use expression templates (such as boost::ublas). Is there any way of designing expression templates to prevent the compiler from compiling such code that may result in the use of expired temporaries at runtime? (I've attempted unsuccessfully to work around this issue, the attempt is here) 回答1: Is there