class-template

class template constructor overload resolution ambiguity

荒凉一梦 提交于 2021-02-10 12:41:30
问题 I'm writing a class template like stl vector,and two constructors are like this: template<class T> vector<T>::vector(size_t count, const T&value) :bg(new T[count]), ed(bg + count), cap(ed) { for (auto it = bg; it != ed; ++it) *it = value; }//bg ed cap are all T* template<class T> template<class Input> vector<T>::vector(Input first, Input second) : bg(new T[second - first]), ed(bg + (second - first)), cap(ed) { memcpy(bg, (void*)first, sizeof(T)*(second - first)); } so if I do this vector<int

class template constructor overload resolution ambiguity

别来无恙 提交于 2021-02-10 12:40:35
问题 I'm writing a class template like stl vector,and two constructors are like this: template<class T> vector<T>::vector(size_t count, const T&value) :bg(new T[count]), ed(bg + count), cap(ed) { for (auto it = bg; it != ed; ++it) *it = value; }//bg ed cap are all T* template<class T> template<class Input> vector<T>::vector(Input first, Input second) : bg(new T[second - first]), ed(bg + (second - first)), cap(ed) { memcpy(bg, (void*)first, sizeof(T)*(second - first)); } so if I do this vector<int

class template constructor overload resolution ambiguity

末鹿安然 提交于 2021-02-10 12:37:04
问题 I'm writing a class template like stl vector,and two constructors are like this: template<class T> vector<T>::vector(size_t count, const T&value) :bg(new T[count]), ed(bg + count), cap(ed) { for (auto it = bg; it != ed; ++it) *it = value; }//bg ed cap are all T* template<class T> template<class Input> vector<T>::vector(Input first, Input second) : bg(new T[second - first]), ed(bg + (second - first)), cap(ed) { memcpy(bg, (void*)first, sizeof(T)*(second - first)); } so if I do this vector<int

Why can't you partially specialize a class member function?

て烟熏妆下的殇ゞ 提交于 2021-01-29 05:03:36
问题 Member functions of template classes can be fully specialized, e.g. template<class A> struct MyClass { // Lots of other members int foo(); }; template<class A> MyClass<A>::foo() { return 42; } template<> MyClass<int>::foo() { return 0; } would compile without problems. Note that foo() is not a template function so this is not about template function specialization (I can understand partial specialization is not allowed there as it would become incredibly confusing in combination with

Can not access member type from base-class when compiled using c++17 [duplicate]

放肆的年华 提交于 2020-08-17 05:39:18
问题 This question already has answers here : Why do I have to access template base class members through the this pointer? (3 answers) Closed 7 months ago . I have the following code which compiles successfully in c++14. template<class T, class ...Args> class B { public: using AbcData = int; }; template<typename ...Args> class D : public B<float, Args...> { public: AbcData m_abc; }; But when compiled in c++17, it gives the following error. error C2061: syntax error: identifier 'AbcData' What is

Can not access member type from base-class when compiled using c++17 [duplicate]

老子叫甜甜 提交于 2020-08-17 05:38:53
问题 This question already has answers here : Why do I have to access template base class members through the this pointer? (3 answers) Closed 7 months ago . I have the following code which compiles successfully in c++14. template<class T, class ...Args> class B { public: using AbcData = int; }; template<typename ...Args> class D : public B<float, Args...> { public: AbcData m_abc; }; But when compiled in c++17, it gives the following error. error C2061: syntax error: identifier 'AbcData' What is

Will consteval functions allow template parameters dependent on function arguments?

孤街浪徒 提交于 2020-03-10 10:50:02
问题 In C++17, this code is illegal: constexpr int foo(int i) { return std::integral_constant<int, i>::value; } That's because even if foo can be evaluated at compile-time, the compiler still needs to produce the instructions to execute it at runtime, thus making the template instantiation impossible. In C++20 we will have consteval functions, which are required to be evaluated at compile-time, so the runtime constraint should be removed. Does it mean this code will be legal? consteval int foo(int