templates

templated recursive data types

主宰稳场 提交于 2021-02-07 05:33:47
问题 I have a recursive data type like this: template<typename T> struct SomeType { std::map<T, SomeType<T>> mapping; }; SomeType<int> foo; This works fine, but replacing std::map with std::unordered_map results in a compile error due to an incomplete type. Am I (or gcc) making an error somewhere? or is this just part of the standard? I would also like to have the internal container determined by a template parameter (like std::stack and std::queue ), but I can't figure out a way to do it since

templated recursive data types

拈花ヽ惹草 提交于 2021-02-07 05:33:18
问题 I have a recursive data type like this: template<typename T> struct SomeType { std::map<T, SomeType<T>> mapping; }; SomeType<int> foo; This works fine, but replacing std::map with std::unordered_map results in a compile error due to an incomplete type. Am I (or gcc) making an error somewhere? or is this just part of the standard? I would also like to have the internal container determined by a template parameter (like std::stack and std::queue ), but I can't figure out a way to do it since

C++ CRTP class hierarchy

て烟熏妆下的殇ゞ 提交于 2021-02-07 03:50:14
问题 From Wikipedia: // The Curiously Recurring Template Pattern (CRTP) template <typename T> struct base { // ... }; struct derived : base<derived> { // ... }; Now if I want derived_from_derived , I can write: // The Curiously Recurring Template Pattern (CRTP) template <typename T> struct base { // ... }; template <typename T> struct derived : base<T> { // ... }; struct derived_from_derived : derived <derived_from_derived> { // ... }; Now suppose I just want a derived object. This doesn't work:

Eliminate redundant template argument in C++

南楼画角 提交于 2021-02-07 03:46:47
问题 I'm trying to write a demo that implements the fmap in Haskell with continuation , and my code looks like this: #include <cstdio> #include <functional> template <typename X> using Callback = std::function<void(X)>; template <typename X, typename Y> using Fun = std::function<Y(X)>; template <typename X, typename Y> struct F_map; template <typename X> struct __F { virtual void operator()(Callback<X>&& callback) = 0; virtual __F<X>* self() { return this; } template <typename Y> auto map(Fun<X, Y

C++ CRTP class hierarchy

半世苍凉 提交于 2021-02-07 03:42:56
问题 From Wikipedia: // The Curiously Recurring Template Pattern (CRTP) template <typename T> struct base { // ... }; struct derived : base<derived> { // ... }; Now if I want derived_from_derived , I can write: // The Curiously Recurring Template Pattern (CRTP) template <typename T> struct base { // ... }; template <typename T> struct derived : base<T> { // ... }; struct derived_from_derived : derived <derived_from_derived> { // ... }; Now suppose I just want a derived object. This doesn't work:

c++17 efficiently multiply parameter pack arguments with std::array elements

僤鯓⒐⒋嵵緔 提交于 2021-02-07 02:51:15
问题 I want to efficiently multiply the arguments from a parameter pack with the elements of a std::array: int index(auto... Is, std::array<int,sizeof...(Is)> strides) { // pseudo-code // int idx = 0; // for(int i = 0; i < sizeof...(Is); ++i) // idx += Is[i] * strides[i]; // return idx; } I can't quite wrap my brain around this one. I started down the road of an index sequence, but I could figure out how to incorporate the summation. I am using c++17, so fold expressions are fair game if they

c++17 efficiently multiply parameter pack arguments with std::array elements

流过昼夜 提交于 2021-02-07 02:50:55
问题 I want to efficiently multiply the arguments from a parameter pack with the elements of a std::array: int index(auto... Is, std::array<int,sizeof...(Is)> strides) { // pseudo-code // int idx = 0; // for(int i = 0; i < sizeof...(Is); ++i) // idx += Is[i] * strides[i]; // return idx; } I can't quite wrap my brain around this one. I started down the road of an index sequence, but I could figure out how to incorporate the summation. I am using c++17, so fold expressions are fair game if they

c++17 efficiently multiply parameter pack arguments with std::array elements

拥有回忆 提交于 2021-02-07 02:50:41
问题 I want to efficiently multiply the arguments from a parameter pack with the elements of a std::array: int index(auto... Is, std::array<int,sizeof...(Is)> strides) { // pseudo-code // int idx = 0; // for(int i = 0; i < sizeof...(Is); ++i) // idx += Is[i] * strides[i]; // return idx; } I can't quite wrap my brain around this one. I started down the road of an index sequence, but I could figure out how to incorporate the summation. I am using c++17, so fold expressions are fair game if they

Static duck typing in C++

眉间皱痕 提交于 2021-02-07 02:39:56
问题 C++ has some sort of duck typing for types given by template parameters. We have no idea what type DUCK1 and DUCK2 will be, but as long as they can quack() , it will compile and run: template <class DUCK1, class DUCK2> void let_them_quack(DUCK1* donald, DUCK2* daisy){ donald->quack(); daisy->quack(); } But it's a bit inconvenient to write. When I do absolutely not care what actual types DUCK1 and DUCK2 are but rather want to fully use the idea of duck typing, then I would like to have

Function template overload resolution, dependent and non-dependent parameters

爷,独闯天下 提交于 2021-02-07 02:21:39
问题 Given the following program #include <iostream> template<class T> struct id { using type = T; }; template<class T1, class T2> int func(T1, T2) { return 0; } template<class T1, class T2> int func(typename id<T1>::type, typename id<T2>::type) { return 1; } int main() { std::cout << func<int, int>(0, 0) << std::endl; } GCC and Clang both prints 1 for this program. Is this program guaranteed to print 1 by the standard? I tried finding the answer here but couldn't decipher it. It looks like the