templates

Multiple parameter packs — how?

我只是一个虾纸丫 提交于 2021-02-20 10:13:06
问题 I have the following problem: #include <vector> #include <tuple> using namespace std; template< size_t... N_i, typename Ts... > class A { // ... private: std::vector<size_t> _v = { N_i... }; std::tuple<Ts...> _t; }; int main() { A<1> a; } As you can see above, I try to define multiple parameter packs as template arguments of the class A . Unfortunately, the code does not compile: error: expected nested-name-specifier before 'Ts' How can I define multiple parameter packs for this example? 回答1:

Multiple parameter packs — how?

非 Y 不嫁゛ 提交于 2021-02-20 10:10:15
问题 I have the following problem: #include <vector> #include <tuple> using namespace std; template< size_t... N_i, typename Ts... > class A { // ... private: std::vector<size_t> _v = { N_i... }; std::tuple<Ts...> _t; }; int main() { A<1> a; } As you can see above, I try to define multiple parameter packs as template arguments of the class A . Unfortunately, the code does not compile: error: expected nested-name-specifier before 'Ts' How can I define multiple parameter packs for this example? 回答1:

Multiple parameter packs — how?

孤者浪人 提交于 2021-02-20 10:06:51
问题 I have the following problem: #include <vector> #include <tuple> using namespace std; template< size_t... N_i, typename Ts... > class A { // ... private: std::vector<size_t> _v = { N_i... }; std::tuple<Ts...> _t; }; int main() { A<1> a; } As you can see above, I try to define multiple parameter packs as template arguments of the class A . Unfortunately, the code does not compile: error: expected nested-name-specifier before 'Ts' How can I define multiple parameter packs for this example? 回答1:

Multiple parameter packs — how?

穿精又带淫゛_ 提交于 2021-02-20 10:06:23
问题 I have the following problem: #include <vector> #include <tuple> using namespace std; template< size_t... N_i, typename Ts... > class A { // ... private: std::vector<size_t> _v = { N_i... }; std::tuple<Ts...> _t; }; int main() { A<1> a; } As you can see above, I try to define multiple parameter packs as template arguments of the class A . Unfortunately, the code does not compile: error: expected nested-name-specifier before 'Ts' How can I define multiple parameter packs for this example? 回答1:

How to initialize a constexpr std::array with templated constexpr member functions?

走远了吗. 提交于 2021-02-19 08:45:38
问题 This is a follow up of my question given here. In the end I want to create a constexpr std::array containing text with an appended running index. I wanted to try a different approach than in the previous question. Nearly everything, what I do in the below code is constexpr. But maybe, it is simply the old problem of returning a pointer to a no longer existing variable. But, I doubt this. Please see the following code, where the not working line in function main is marked. #include <iostream>

c++ log functions using template SFINAE for conditional compile

前提是你 提交于 2021-02-19 08:34:44
问题 I am evaluating if it is possible to leverage C++11 features to replace logging Macros without any run-time additional cost. I come out with this demo: enum class LogLevel { Fatal = 0, DFatal = 1, Error = 2, Normal = 3, Verbose = 4, Debug = 5 }; constexpr LogLevel log_compiled = LogLevel::Normal; LogLevel log_runtime = LogLevel::Error; #ifdef NDEBUG constexpr LogLevel log_fatal = LogLevel::Fatal; #else constexpr LogLevel log_fatal = LogLevel::DFatal; #endif template <LogLevel L, typename std:

Extend a template classe using the type definition in subclass

我们两清 提交于 2021-02-19 08:13:07
问题 I imitated the std::enable_shared_from_this to create a template class, but I made the class use the type definition in its subclass. Unfortunately! Although I used typename , after compiling, // // https://ideone.com/eYCBHW http://ideone.com/eYCBHW #include <iostream> #include <set> #include <map> using namespace std; template<class _S> struct A { }; template<class _Subclass> class Global { public: typedef typename _Subclass::connection_t connection_t; //std::map<std::string, _Subclass:

A container of std::function with polymorphic types as function arguments

我与影子孤独终老i 提交于 2021-02-19 05:22:17
问题 I would like to have "yet another" callback registration stuff. Different event types extending a common base event type will trigger associated callback functions. here is the initial draft or idea #include <functional> #include <iostream> #include <unordered_map> class BaseEvent { public: virtual ~BaseEvent() {} }; class DerivedEvent_1 : public BaseEvent {}; class DerivedEvent_2 : public BaseEvent {}; // a container holding callback functions std::unordered_map<size_t/*event*/, std:

gcc doesn't accept out-of-line definition of member with non-type template parameter defined via nested templated using clause

戏子无情 提交于 2021-02-19 04:07:51
问题 The title seems convoluted but our test-case is actually a minimal example for a real case. We have code for which we want to choose implementation of a method based on the template parameter. We during clean up we defined conditional enable_if_t with using clause, and as next step wanted to put definition out of line, this produced following code: #include <type_traits> #include <iostream> #include <string> template <typename T> struct A { template <typename U> using is_int_or_float = std:

gcc doesn't accept out-of-line definition of member with non-type template parameter defined via nested templated using clause

混江龙づ霸主 提交于 2021-02-19 04:07:34
问题 The title seems convoluted but our test-case is actually a minimal example for a real case. We have code for which we want to choose implementation of a method based on the template parameter. We during clean up we defined conditional enable_if_t with using clause, and as next step wanted to put definition out of line, this produced following code: #include <type_traits> #include <iostream> #include <string> template <typename T> struct A { template <typename U> using is_int_or_float = std: