template-meta-programming

How to detect the presence of a static member function with certain signature?

牧云@^-^@ 提交于 2019-11-28 01:54:00
问题 I found several questions & answers on SO dealing with detecting at compile time (via SFINAE) whether a given class has a member of certain name, type, or signature. However, I couldn't find one that also applies to static public member functions (when pointer-to-member tricks won't work). Any ideas? 回答1: Following may help: (https://ideone.com/nDlFUE) #include <cstdint> #define DEFINE_HAS_SIGNATURE(traitsName, funcName, signature) \ template <typename U> \ class traitsName \ { \ private: \

Compile-time constant id

随声附和 提交于 2019-11-27 18:57:51
Given the following: template<typename T> class A { public: static const unsigned int ID = ?; }; I want ID to generate a unique compile time ID for every T. I've considered __COUNTER__ and the boost PP library but have been unsuccessful so far. How can I achieve this? Edit: ID has to be usable as the case in a switch statement Edit2: All the answers based on the address of a static method or member are incorrect. Although they do create a unique ID they are not resolved in compile time and therefore can not be used as the cases of a switch statement. This is sufficient assuming a standards

“too many template-parameter-lists” error when specializing a member function

走远了吗. 提交于 2019-11-27 18:01:15
问题 I would like to define some template member methods inside a template class like so: template <typename T> class CallSometing { public: void call (T tObj); // 1st template <typename A> void call (T tObj, A aObj); // 2nd template <typename A> template <typename B> void call (T tObj, A aObj, B bObj); // 3rd }; template <typename T> void CallSometing<T>::call (T tObj) { std::cout << tObj << ", " << std::endl; } template <typename T> template <typename A> void CallSometing<T>::call (T tObj, A

Using Boost::odeint with Eigen::Matrix as state vector

◇◆丶佛笑我妖孽 提交于 2019-11-27 17:53:30
问题 I'm trying to utilize the ODE integration capabilities of Boost using the Matrix class from Eigen 3 as my state vector, but I'm running into problems deep into Boost that I don't understand how to address. A minimal example of what I'm trying to do: #include <Eigen/Core> #include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp> #include <iostream> using namespace Eigen; using namespace boost::numeric::odeint; template<size_t N> using vector = Matrix<double, N, 1>; typedef vector<3> state

Is it possible to develop static for loop in c++?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 17:45:15
Is it possible for something like this to exist? template<int Channel> void deduce_mask(Matrix const &src, int mask[]) { //I hope i could become a constant and the compiler would unroll the loop at compile time for(int i = Channel; i != -1; --i) { //mapper is a helper class which translate two and three dimension into one dimension index //constexpr makes it possible to find out the index at compile time mask[mapper(0, 1, i)] = src(row - 1, col)[i]; mask[mapper(1, 1, i)] = src(row, col)[i]; mask[mapper(2, 1, i)] = src(row + 1, col)[i]; } } instead of template<int Channel> class deduceMask {

Choose template based on run-time string in C++

半腔热情 提交于 2019-11-27 15:00:43
问题 I have an attribute vector that can hold different types: class base_attribute_vector; // no template args template<typename T> class raw_attribute_vector : public base_attribute_vector; raw_attribute_vector<int> foo; raw_attribute_vector<std::string> foo; Based on run-time input for the type, I would like to create the appropriate data structure. Pseudocode: std::string type("int"); raw_attribute_vector<type> foo; Obviously, this fails. An easy, but ugly and unmaintainable workaround is a

Why can't std::tuple<int> be trivially copyable?

ε祈祈猫儿з 提交于 2019-11-27 14:58:55
问题 Built with this online compiler, the following code: #include <iostream> #include <type_traits> #include <tuple> int main() { std::cout << std::is_trivially_copyable<std::tuple<int>>::value << std::endl; std::cout << std::is_trivially_copyable<std::pair<int, int>>::value << std::endl; std::cout << std::is_trivial<std::tuple<int>>::value << std::endl; std::cout << std::is_trivial<std::pair<int, int>>::value << std::endl; return 0; } outputs: 0 0 0 0 I'm getting the same results with Visual

constexpr initialization of array to sort contents

回眸只為那壹抹淺笑 提交于 2019-11-27 14:05:55
问题 This is a bit of a puzzle rather than a real-world problem, but I've gotten into a situation where I want to be able to write something that behaves exactly like template<int N> struct SortMyElements { int data[N]; template<typename... TT> SortMyElements(TT... tt) : data{ tt... } { std::sort(data, data+N); } }; int main() { SortMyElements<5> se(1,4,2,5,3); int se_reference[5] = {1,2,3,4,5}; assert(memcmp(se.data, se_reference, sizeof se.data) == 0); } except that I want the SortMyElements

C++ iterate into nested struct field with boost fusion adapt_struct

℡╲_俬逩灬. 提交于 2019-11-27 12:56:05
Two stackoverflow answers suggest the approach using fusion adapt_struct to iterate over struct fields. The approach looks nice. However, how do you iterate into a field which itself is a struct? Following the previous answers, I come up with the code below. The problem is at the "#if 0" clause the code does not compile. As an alternative solution I created "decode()" function to take a void pointer to the target argument. That works, but loses the type information at compile time. Is there a better solution? struct Foo_s { int i; }; BOOST_FUSION_ADAPT_STRUCT( Foo_s, (int, i) ) struct Bar_s {

Concatenate compile-time strings in a template at compile time?

*爱你&永不变心* 提交于 2019-11-27 12:36:20
问题 Currently I have: template <typename T> struct typename_struct<T*> { static char const* name() { return (std::string(typename_struct<T>::name()) + "*").c_str(); } }; I wonder if I can avoid the whole bit where I'm forced to allocate a string to perform the concatenation. This is all happening at compile time, i.e. I intend to get the string "int****" when I reference typename_struct<int****>::name() . (Do assume that I have declared a corresponding specialization for int which returns "int" )