dependent-name

template base class typedef members invisible

馋奶兔 提交于 2020-12-19 15:13:38
问题 I'm aware of the fact that the 'dependent names' are not visible to the compiler by default. But I was told in answers to other SO questions (here, here, and ultimately on the C++ faq) that a using declaration may help. So I tried. A template base class: // regardless of the fact that members are exposed... template<typename T> struct TBase { typedef T MemberType; MemberType baseMember; MemberType baseFunction() { return MemberType(); } }; And a derived class, using the base's members:

template base class typedef members invisible

拥有回忆 提交于 2020-12-19 15:07:31
问题 I'm aware of the fact that the 'dependent names' are not visible to the compiler by default. But I was told in answers to other SO questions (here, here, and ultimately on the C++ faq) that a using declaration may help. So I tried. A template base class: // regardless of the fact that members are exposed... template<typename T> struct TBase { typedef T MemberType; MemberType baseMember; MemberType baseFunction() { return MemberType(); } }; And a derived class, using the base's members:

Where and why do I have to put the “template” and “typename” keywords?

徘徊边缘 提交于 2020-03-03 06:57:46
问题 In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway? I have the following code: template <typename T, typename Tail> // Tail will be a UnionNode too. struct UnionNode : public Tail { // ... template<typename U> struct inUnion { // Q: where to add typename/template here? typedef Tail::inUnion<U> dummy; }; template< > struct inUnion<T> { }; }; template <typename T> // For the last node Tn. struct UnionNode<T, void> { //

Why can a dependent name be considered as complete even if the actual type is not defined until the very end

允我心安 提交于 2020-01-13 08:20:09
问题 Consider this example: template <class T> void Yeap(T); int main() { Yeap(0); return 0; } template <class T> void YeapImpl(); struct X; template <class T> void Yeap(T) { YeapImpl<X>(); // pass X to another template } template <class T> void YeapImpl() { T().foo(); } struct X { void foo() {} }; Note that struct X is not defined until the very end. I used to believe that all odr-used names must be complete at the point of the instantiation. But here, how can the compiler treat it as a complete

dependent types with variadic templates

风格不统一 提交于 2020-01-04 02:54:07
问题 Can you see anything wrong with this function declaration? template<typename... Containers> std::tuple<typename Containers::value_type...> foo(const Containers &...args); When I try to call it, like this: foo(std::list<int>(), std::vector<float>()); MSVC2013 says error C2027: use of undefined type 'std::tuple<Containers::value_type> . I tried rewriting the function declaration with the "late return" syntax and it made no difference. Is there any way I can achieve what this code is trying to

multiple nested dependent names - where to stick the typename keyword?

拜拜、爱过 提交于 2019-12-21 03:55:37
问题 This question was inspired by this other question. While trying to answer that question, I understood that I have a lot of questions myself. So... Consider the following: struct S1 { enum { value = 42 }; }; template <class T> struct S2 { typedef S1 Type; }; template <class T> struct S3 { typedef S2<T> Type; }; template <class T> struct S4 { typedef typename T::Type::Type Type; //(1)//legal? enum {value = T::Type::Type::value }; //(2)//legal? }; int main() { S4<S3<S2<S2<S1> > > >::value; }

Where and why do I have to put the “template” and “typename” keywords?

空扰寡人 提交于 2019-12-13 03:24:54
问题 In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway? I have the following code: template <typename T, typename Tail> // Tail will be a UnionNode too. struct UnionNode : public Tail { // ... template<typename U> struct inUnion { // Q: where to add typename/template here? typedef Tail::inUnion<U> dummy; }; template< > struct inUnion<T> { }; }; template <typename T> // For the last node Tn. struct UnionNode<T, void> { //

Computing the type of a function pointer

牧云@^-^@ 提交于 2019-12-13 02:44:59
问题 Consider the following: template<typename T> struct S { typedef M< &T::foo > MT; } This would work for: S<Widget> SW; where Widget::foo() is some function How would I modify the definition of struct S to allow the following instead: S<Widget*> SWP; 回答1: What you need is the following type transformation. given T , return T given T * , return T It so happens that the standard library already has implemented this for us in std::remove_pointer (though it's not hard to do yourself). With this,

Where and why do I have to put the “template” and “typename” keywords?

江枫思渺然 提交于 2019-12-11 17:13:16
问题 In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway? I have the following code: template <typename T, typename Tail> // Tail will be a UnionNode too. struct UnionNode : public Tail { // ... template<typename U> struct inUnion { // Q: where to add typename/template here? typedef Tail::inUnion<U> dummy; }; template< > struct inUnion<T> { }; }; template <typename T> // For the last node Tn. struct UnionNode<T, void> { //

Comparison operator for std::vector<T> fails to find comparison operator for T

廉价感情. 提交于 2019-12-09 09:09:09
问题 The following very simple code won't compile #include <vector> #include <string> namespace Foobar { struct Test { std::string f; std::uint16_t uuid; }; } bool operator==(const Foobar::Test& lhs, const Foobar::Test& rhs){ return lhs.f == rhs.f && lhs.uuid == rhs.uuid; } int main(){ std::vector<Foobar::Test> a; std::vector<Foobar::Test> b; if(a==b){ } return 0; } https://godbolt.org/g/zn6UgJ Won't compile in any of the compilers I have. While the following #include <vector> #include <string>