variadic-templates

Store arbitrary elements in contiguous memory

六月ゝ 毕业季﹏ 提交于 2019-12-12 17:12:01
问题 I am trying to create a data structure, where it will hold N number of different types in contiguous memory. So at compile time I can say I want to store 4 elements of 3 different types, and in memory it will look like 111122223333. I've been going with a variadic template approach, which I think will do what I want, however I am not sure how to add the elements to each array in the add method. template<std::size_t N, typename... Args> class Batch { private: std::tuple<std::array<Args, N>...>

C++ template parameter as function call name

旧巷老猫 提交于 2019-12-12 16:04:58
问题 In C++, is possible to make something like this: class A { public: template <typename ClassType, typename MethodName, typename ...Args> static func() { ClassType t = GetPtr<ClassType>(); t->MethodName(GetArg<Args>()...); } } A::func<B, sum, double, double>(); //should call B->sum(double, double) A::func<C, sum2, std::string, double, double>(); //should call C->sum2(std::string, double, double) where GetPtr and GetArgs are working methods to obtain pointers from dictionary. 回答1: You could make

Why does gcc complain “error: type 'intT' of template argument '0' depends on a template parameter”?

孤人 提交于 2019-12-12 15:54:10
问题 My compiler is gcc 4.9.0. The following code cannot be compiled: template<typename T, T i> struct value {}; template<typename T> struct value<T, 0> {}; // error: type 'T' of template argument '0' depends on a template parameter What is the cause? and, how to solve this issue? 回答1: GCC is right, this is explicitly forbidden by C++11 [temp.class.spec] §8: 8 Within the argument list of a class template partial specialization, the following restrictions apply: A partially specialized non-type

What's the most efficient tail recursive prime verification function known?

那年仲夏 提交于 2019-12-12 13:19:52
问题 I was experimenting with meta programming to this point: // compiled on Ubuntu 13.04 with: // clang++ -O3 -ftemplate-depth-8192 -fconstexpr-depth=4096 -std=c++11 -stdlib=libc++ -lcxxrt -ldl compile-time-primes.cpp -o compile-time-primes // assembly output with: // clang++ -S -mllvm --x86-asm-syntax=intel -O3 -ftemplate-depth-8192 -fconstexpr-depth=4096 -std=c++11 -stdlib=libc++ -lcxxrt -ldl compile-time-primes.cpp -o compile-time-primes.asm #include <array> #include <iostream> template

Why 'enable_if' cannot be used to disable this declaration here

瘦欲@ 提交于 2019-12-12 13:05:51
问题 #include<string> #include<type_traits> template<typename... Args> class C { public: void foo(Args&&... args) { } template<typename = std::enable_if_t<(0 < sizeof...(Args))>> void foo(const Args&... args) { } }; int main() { C<> c; c.foo(); return 0; } Above code works as expacted (by me :)) and calls void foo(Args&&... args) at run-time in msvc 2015 but same code fails to even compile in both gcc 7.3 and clang 6.0.0 with error: error: no type named 'type' in 'std::enable_if'; 'enable_if'

C++ variadic templates and evaluation order

China☆狼群 提交于 2019-12-12 12:35:32
问题 I have the following code: lib.hxx: template <typename C, typename R, typename ... Args> R Lib::extract_call(lua_State* L, R(C::*method)(Args...)) { return static_cast<C*>(this)->*method(extract_data<Args>(L)...); } lib.cc: template <> std::string Lib::extract_data(lua_State* L) { if (! lua_isstring(L, -1)) { return ""; } return lua_tostring(L, -1); } [...] // Other specializations following I am embedding lua in a project, and I'm currently looking for a way to call methods from lua, and

Linker error for variadic template

廉价感情. 提交于 2019-12-12 12:31:19
问题 I have a program with variadic templates and a helper function: #include <iostream> #include <string> using std::cout; template<typename... Ts> void fooImpl(char const *cp, Ts... args); template<typename... Ts> inline void foo(const std::string &s, Ts... args) { fooImpl(s.c_str(), args...); } void fooImpl(char const *cp) { // do something } template<typename T, typename... Ts> void fooImpl(char const *cp, T val, Ts... args) { char special{'@'}; while (*cp) { if (*cp == special) { // handle

Variadic template-based multiple inheritance for two interacting classes…

蹲街弑〆低调 提交于 2019-12-12 12:26:58
问题 In my current project, I need to be able to provide template-based multiple inheritance (Mixin pattern) and have two classes (with mirroring multiple-inheritance trees) that can interact together (i.e. one uses methods from the other one at the same inheritance level). Long story short, I cannot seem to find an elegant way to build this. Below is a reduced testcase (you can run and edit it directly here). Is there a pattern or trick that would let me have something similar to the commented

Initializing std::array with Static Storage Duration with a Parameter Pack Expansion and an Additional Value

我是研究僧i 提交于 2019-12-12 12:23:38
问题 While asking another question recently, I stumbled upon some strange behavior of GCC when initializing a std::array with a parameter pack expansion followed by another element . I have already discussed this briefly with Jarod42 in the comments there but I believe it should better be asked as a new question. For example, consider the following code that is supposed to provide a utility make_array function that takes an arbitrary number of parameters and std::forward s them to the std::array

Implement STL functions in variadic template

≯℡__Kan透↙ 提交于 2019-12-12 12:07:41
问题 I have been working on a small project to get up to speed with variadic templates. I implemented a small multidimensional array. I would now like to define a function that operates on the nearest neighbours of a given position -- is there an elegant way to retrieve the values of the neighbours of a given position in my array? template<class T, size_t size, size_t... sizes> struct MArr { typedef std::array<typename MArr<T, sizes...>::type, size> type; std::array<MArr<T, sizes...>,size> data;