templates

Mixing pass-by-reference and pass-by-value to variadic template function valid?

此生再无相见时 提交于 2021-02-08 14:11:27
问题 I have a method which allocates memory for a object and then calls its constructor - a memory allocator. template <class T, typename... Arguments> inline T* AllocateObject(Arguments... args) { return new (InternalAllocate(sizeof(T))) T(args...); } Is it valid using this function to mix pass-by-value and pass-by-reference? For example allocating a class with a constructor with some by-value and some by-reference. It compiles, but I'm not sure if it has any nasty side-effects or not. 回答1: What

Mixing pass-by-reference and pass-by-value to variadic template function valid?

故事扮演 提交于 2021-02-08 14:05:46
问题 I have a method which allocates memory for a object and then calls its constructor - a memory allocator. template <class T, typename... Arguments> inline T* AllocateObject(Arguments... args) { return new (InternalAllocate(sizeof(T))) T(args...); } Is it valid using this function to mix pass-by-value and pass-by-reference? For example allocating a class with a constructor with some by-value and some by-reference. It compiles, but I'm not sure if it has any nasty side-effects or not. 回答1: What

C++ returning nested class with template on base class problem

我的未来我决定 提交于 2021-02-08 12:45:12
问题 I am trying to create a list object, with the iterator class nested inside to understand how it works. In some method, I am trying to return an iterator object but it doesn't work. I created an example to show the problem : // CLASS A template <class T> class A { public: class B; A(){} }; // CLASS B template <class T> class A<T>::B { private: int varB; public: B(B& b); B(const int&); B returnThis(); }; template <class T> A<T>::B::B(const int& value) { varB = value; } template <class T> A<T>:

C++ returning nested class with template on base class problem

早过忘川 提交于 2021-02-08 12:45:08
问题 I am trying to create a list object, with the iterator class nested inside to understand how it works. In some method, I am trying to return an iterator object but it doesn't work. I created an example to show the problem : // CLASS A template <class T> class A { public: class B; A(){} }; // CLASS B template <class T> class A<T>::B { private: int varB; public: B(B& b); B(const int&); B returnThis(); }; template <class T> A<T>::B::B(const int& value) { varB = value; } template <class T> A<T>:

Private member access in template substitution and SFINAE

僤鯓⒐⒋嵵緔 提交于 2021-02-08 12:21:37
问题 class A { int a; }; template<typename, typename = void> class test {}; template<typename T> class test<T,decltype(T::a)> {}; int main() { test<A> a; } The code above compiles without error on clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final) , but fails to compile on g++-5 (Ubuntu 5.4.1-2ubuntu1~16.04) 5.4.1 20160904 and g++-6 (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901 with errors like this: main.cpp: In function ‘int main()’: main.cpp:9:22: error: ‘int A::a’ is private within this

Private member access in template substitution and SFINAE

感情迁移 提交于 2021-02-08 12:20:57
问题 class A { int a; }; template<typename, typename = void> class test {}; template<typename T> class test<T,decltype(T::a)> {}; int main() { test<A> a; } The code above compiles without error on clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final) , but fails to compile on g++-5 (Ubuntu 5.4.1-2ubuntu1~16.04) 5.4.1 20160904 and g++-6 (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901 with errors like this: main.cpp: In function ‘int main()’: main.cpp:9:22: error: ‘int A::a’ is private within this

Private member access in template substitution and SFINAE

橙三吉。 提交于 2021-02-08 12:19:11
问题 class A { int a; }; template<typename, typename = void> class test {}; template<typename T> class test<T,decltype(T::a)> {}; int main() { test<A> a; } The code above compiles without error on clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final) , but fails to compile on g++-5 (Ubuntu 5.4.1-2ubuntu1~16.04) 5.4.1 20160904 and g++-6 (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901 with errors like this: main.cpp: In function ‘int main()’: main.cpp:9:22: error: ‘int A::a’ is private within this

How to deduce the size of a compile time a const char string with C++ templates?

百般思念 提交于 2021-02-08 11:30:48
问题 I am trying to use clang++ to expand the template code from this post. Here is what I come up with. constexpr unsigned int requires_inRange(unsigned int i, unsigned int len) { return i >= len ? throw i : i; } class StrWrap { unsigned size_; char * const begin_; public: template< unsigned N > constexpr StrWrap( const char(&arr)[N] ) : begin_(arr), size_(N - 1) { static_assert( N >= 1, "not a string literal"); } constexpr char operator[]( unsigned i ) { return requires_inRange(i, size_), begin_

How to deduce the size of a compile time a const char string with C++ templates?

不打扰是莪最后的温柔 提交于 2021-02-08 11:30:25
问题 I am trying to use clang++ to expand the template code from this post. Here is what I come up with. constexpr unsigned int requires_inRange(unsigned int i, unsigned int len) { return i >= len ? throw i : i; } class StrWrap { unsigned size_; char * const begin_; public: template< unsigned N > constexpr StrWrap( const char(&arr)[N] ) : begin_(arr), size_(N - 1) { static_assert( N >= 1, "not a string literal"); } constexpr char operator[]( unsigned i ) { return requires_inRange(i, size_), begin_

Create a compile time key-to-type map which is filled by calls to a variadic function

懵懂的女人 提交于 2021-02-08 11:20:44
问题 Is it possible to create a key->type map at compile time, with each key-value being added when an instance of a variadic function is called? template <typename T, typename ... Args> void writeToQueue(Args... args) { //Do something with args. // Insert to map. something akin to: // CODEMAP[T] = Args... // T -> Args... mapped when foo<T,Args...> is called. } Or template <int code, typename ... Args> void writeToQueue(Args... args) { //Do something with args. // Insert to map. something akin to: