English 中文(简体)
我如何能够在模版的班子里获得我nes的班子,以接受其他版本。
原标题:How can I get my nested class inside a templated class to accept other versions of itself?

下面是没有汇编的,我如何能够这样做? 我认为,这个例子表明了我的意图,但如果有人混淆,我就试图增加一个模糊之处。

template<typename T>
class A
{
private:
    struct B
    {
        template<typename T2> 
        B& operator=( const A<T2>::B& right ){} //  how can I make this work?
    };

    template<typename T2> friend class A;
    template<typename T2> friend class A<T2>::B;
};
最佳回答

您的派任经营人的基本想法存在缺陷,因为即使添加了<条码>的类型<> /代码>,该代码为不可替代背景<>。 因此,模板参数将永远不会被推卸,除非你明确指明如下类型:<条码>B.operator=<有些——类型与“其他_B”。

An easier version would be to just make it a normal function template, and SFINAE your way out.

#include <type_traits>

template<class> struct is_a_B;

template<class B2>
typename std::enable_if<
  is_a_B<B2>::value,
  B&
>::type operator=(B2 const& other){
  // ...
}

现在所有左边都是<代码>is_a_B。 海峡。 你们能够以可能的虚假积极态度,使这种情况很容易发生:

template<class B>
struct is_a_B{
  typedef char yes;
  typedef yes (&no)[2];

  template<class T>
  static yes test(typename T::I_am_a_B_type*);
  template<class T>
  static no  test(...);

  static bool const value = sizeof(test<B>(0)) == sizeof(yes);
};

Just provide the I_am_a_B_type 页: 1

http://ideone.com/MYMiH”rel=“nofollow> 关于Ideone的活例子。 评论b1 = 5;行,并汇编成


And now for the slightly more perverted complicated way with no false-positives. :)

template<bool Cond, class OnTrue>
struct and_v{
  static bool const value = OnTrue::value;
};

template<class OnTrue>
struct and_v<false, OnTrue>{
  static bool const value = false;
};

template<class B>
struct is_a_B{
  typedef char yes;
  typedef yes (&no)[2];

  template<class T>
  static yes has_parent(typename T::parent*);
  template<class T>
  static no  has_parent(...);

  template<class T>
  static yes is_A(A<T>*);
  static no  is_A(...);

  template<class T>
  struct lazy_test{
    typedef typename std::add_pointer<typename T::parent>::type p_type;
    static bool const value = sizeof(is_A(p_type(0))) == sizeof(yes);
  };

  static bool const value = and_v<sizeof(has_parent<B>(0)) == sizeof(yes),
                                  lazy_test<B>>::value;
};

For this one you need a typedef A<T> parent; inside B. It s staged in two parts:

  • First I test if a parent typedef exists, and if it does
  • If it s actually a typedef of the A class template.

可悲的是,逻辑操作者(&&,>,?: t t 如同我所希望的那样,在模板代码上不出现短路,因此,我不得不写上<代码>和_v的模板+ a lazy tester,只要有 类型def>,只有经过评估。

http://ideone.com/tyVTo”rel=“nofollow> 关于Ideone的活例子。 http://ideone.com/szZsc” rel=“nofollow”

问题回答

It should not work at all. Here is the reason

template<typename T>
class A
{
    private:
    struct B
    {
        template<typename T2> 
        B& operator=( const typename A<T2>::B& right ){}
    };

    template<typename T2> friend class A;
    template<typename T2> friend class A<T2>::B;
};

template <>
class A<int>
{
};

各位都可以看到: A< int >:B 班次!





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?