c++ - 如何显式实例化具有友元函数嵌套类的模板类 (C++)

标签 c++ templates

可能之前有人问过,但这一切已经接近我对C++的理解和认知的极限,所以我在理解正在谈论的内容和到底发生了什么方面有点慢。让我直接跳到代码。这有效:

template <typename T>
class Foo
{
    struct Bar
    {
        Bar() {}
        ~Bar() noexcept {}
        Bar(Bar&& b) : Bar() { swap(*this, b); }

        friend void swap(Bar& b1, Bar& b2) { /* ... */ }
    };
};

template class Foo<int>; // explicit instantiation of Foo with int type

但是我该如何移动 swap 的定义呢?在Bar之外结构体?如果我这样做:

template <typename T>
class Foo {
    struct Bar {
        // ...
        Bar(Bar&& b) : Bar() { swap(*this, b); } // line 16
        // ...
        template <typename V>
          friend void swap(typename Foo<V>::Bar&, typename Foo<V>::Bar&);
    };
};

template <typename T>
  void swap(typename Foo<T>::Bar& b1, typename Foo<T>::Bar& b2) {} // line 26

template class Foo<int>; // line 31

g++(4.7.1,标志:-Wall -std=c++11)报告:

main.cpp: In instantiation of ‘Foo<T>::Bar::Bar(Foo<T>::Bar&&) 
            [with T = int; Foo<T>::Bar = Foo<int>::Bar]’:
main.cpp:31:16:   required from here
main.cpp:16:28: error: no matching function for call to 
            ‘swap(Foo<int>::Bar&, Foo<int>::Bar&)’
main.cpp:16:28: note: candidate is:
main.cpp:26:6: note: template<class T> void swap(typename Foo<T>::Bar&, 
                                                 typename Foo<T>::Bar&)
main.cpp:26:6: note:   template argument deduction/substitution failed:
main.cpp:16:28: note:   couldn't deduce template parameter ‘T’

我猜 swap 的代码显式实例化时也需要创建 Foo ,这是有道理的,但为什么编译器不能弄清楚 swap(Foo<int>::Bar&...)需要创建?为什么模板替换失败?还是我都弄错了?

更新 1

与:

template <typename T> class Foo;
template <typename T>
  void swap(typename Foo<T>::Bar& b1, typename Foo<T>::Bar& b2);

template <typename T>
class Foo {
    struct Bar {
      Bar(Bar&& b) : Bar() { swap(*this, b); }  // line 19
      friend void swap<>(Foo<T>::Bar& b1, Foo<T>::Bar& b2); // line 20
    };
};

template <typename T>
  void swap(typename Foo<T>::Bar& b1, typename Foo<T>::Bar& b2) {} // line 26

template class Foo<int>; // line 29

g++(4.7.1,标志:-Wall -std=c++11)报告:

main.cpp: In instantiation of ‘struct Foo<int>::Bar’:
main.cpp:29:16:   required from here
main.cpp:20:17: error: template-id ‘swap<>’ for ‘void swap(Foo<int>::Bar&, Foo<int>::Bar&)’ does not match any template declaration
main.cpp: In instantiation of ‘Foo<T>::Bar::Bar(Foo<T>::Bar&&) [with T = int; Foo<T>::Bar = Foo<int>::Bar]’:
main.cpp:29:16:   required from here
main.cpp:19:24: error: no matching function for call to ‘Foo<int>::Bar::Bar()’
main.cpp:19:24: note: candidate is:
main.cpp:19:5: note: Foo<T>::Bar::Bar(Foo<T>::Bar&&) [with T = int; Foo<T>::Bar = Foo<int>::Bar]
main.cpp:19:5: note:   candidate expects 1 argument, 0 provided
main.cpp:19:28: error: no matching function for call to ‘swap(Foo<int>::Bar&, Foo<int>::Bar&)’
main.cpp:19:28: note: candidate is:
main.cpp:26:8: note: template<class T> void swap(typename Foo<T>::Bar&, typename Foo<T>::Bar&)
main.cpp:26:8: note:   template argument deduction/substitution failed:
main.cpp:19:28: note:   couldn't deduce template parameter ‘T’

更新 2

好的,所以这是不可能的。 Piotr 已链接到 Output a nested class inside a template ,但我不明白答案。为什么不能swap在其声明之外定义?据我(错误)理解,为什么编译器不能为 swap(Foo<int>::Bar&...) 创建代码并在代码中链接到它以显式实例化 Foo<int> ?我完全误解了发生了什么吗?有什么问题?

更新 3

好的,这不能完成,因为如果有模板特化,编译器不能保证调用swap。在 Foo 之外定义自 Foo<some_class>::Bar 以来是明确的在特定专业中可能是完全不同的东西。我希望我做对了。但是,为什么在我创建 Foo 的显式实例化之前 g++ 不警告我这一点? ?

template <typename T>
class Foo {
    struct Bar {
        // ...
        Bar(Bar&& b) : Bar() { swap(*this, b); }
        // ...
        template <typename V>
          friend void swap(typename Foo<V>::Bar&, typename Foo<V>::Bar&);
    };
};

template <typename T>
  void swap(typename Foo<T>::Bar& b1, typename Foo<T>::Bar& b2) {}

//template class Foo<int>; // let's comment this explicit instantiation out.

此代码编译良好(g++ 4.7.1,标志:-Wall -std=c++11)。但是,它不应该警告我这段代码可能会导致问题吗?当我添加 Foo 的显式实例化时,问题不在于该行本身,而在于 swapFoo 之外实现的代码.

最佳答案

问题不在于 friend .

问题出在这个函数本身:

template <typename T>
void swap(typename Foo<T>::Bar& b1, typename Foo<T>::Bar& b2) {} // line 26

推导模板参数T来自嵌套类是不可能的,请参阅:Output a nested class inside a template甚至更好的答案是:https://stackoverflow.com/a/4092248/1463922

举例说明为什么它不能完成,考虑这个函数:

template <class T>
void foo(typename A<T>::Bar);

还有这个 A 定义:

template <class T>
struct A { typedef int Bar; };

这个电话:

int a;
foo(a);

什么是 T在这个例子中?是int因为A<int>::Barint , 或 float因为 A<float>::Barint太或任何你想要的......问题是你正在调用什么功能foo<int>(int)foo<float>(int)或者……

或者举个更接近问题的例子:

template <class T>
struct Foo {
   struct Bar {}; 
};

看来编译器在解决这个问题上应该没有问题:

template <class T>
void resolve(typename Foo<T>::Bar*);

但编译器即使在这里也有问题,因为它不确定某些其他类的特化是否不会使用其他类的内部结构,如下所示:

template <class T>
struct Foo<T*> {
   typedef Foo<T>::Bar Bar; 
};

因此:

Foo<void>::Bar b;
resolve(&b);

编译器没有机会知道调用哪个版本:

resolve<void>(Foo<void>::Bar*);
// or 
resolve<void*>(Foo<void>::Bar*);
//          ^   

我能给你什么建议 - 使用内联 friend - 但用其他一些模板类来实现它。这行得通 - 但我确信这有点过度设计:

template <class S>
class ImplementSwap;

template <typename T>
class Foo {
    public:
    struct Bar {
        int a;
        Bar() {}
        ~Bar() {}
        friend class ImplementSwap<Foo<T>>;
        friend void swap(Foo<T>::Bar& b1, Foo<T>::Bar& b2)
        {  ImplementSwap<Foo<T>>::doSwap(b1, b2); }
        Bar(Bar&& b)  { swap(*this, b); }

    };
};

template <class T>
class ImplementSwap<Foo<T>> {
public:
   static void doSwap(typename Foo<T>::Bar&,typename Foo<T>::Bar&);
};

template <class T>
void ImplementSwap<Foo<T>>::doSwap(typename Foo<T>::Bar&,typename Foo<T>::Bar&) 
{
  // this one is not inline....
}

我公开了 Bar 来做这个测试:

Foo<int>::Bar a = Foo<int>::Bar(); // move constructor

int main() {
  swap(a,a); // explicit swap
}

<罢工> [老的] 我之前的回答是完全错误的,第一条评论引用它。

<罢工>
friend void swap<>(typename Foo<T>::Bar&, typename Foo<T>::Bar&);
//              ^^

<罢工>[/OLD]

关于c++ - 如何显式实例化具有友元函数嵌套类的模板类 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12875679/

相关文章:

c++ - 给定一组点,找到与查询点曼哈顿距离最小的点

c++ - 对程序的编译时属性进行基准测试

templates - Ansible 取消保管和模板化文件

c++ - GCC 4.2 模板奇怪的错误

c++ - CLion Cmake mysql.h 误解

c++ - QException 的目的是什么?

c# - Protobuf-net(反)小数序列化在使用自定义小数原型(prototype)合约(C#/C++ 互操作)时抛出

python - Python 的模板系统或语言(主要是 HTML)

与 typedef 一起使用时出现 C++ 模板错误

c++ - 如何使用 cppzmq 从 ROUTER 套接字向特定的 DEALER 套接字发送 ZeroMQ 消息?