草庐IT

c++ - 外部模板类和非模板友元函数

coder 2024-02-26 原文

我已经开始尝试使用 extern 模板,但我偶然发现了一个我找不到任何相关信息的问题。假设我有一个带有非模板友元函数的类模板(在类模板声明中定义)。我为该类声明了一些 extern 模板实例化,但是如何将友元函数也声明为 extern?

下面是一些示例代码:

// --- test.h ---

template <typename T>
class Foo {
  private:
    T value;
  public:
    friend void some_friend_function(Foo<T>& obj) {
      obj.value -= T(42);
    };

    void some_member_function(T rhs) { value += rhs; };

};

extern template class Foo<int>;
//extern void some_friend_function(Foo<int>&);  // I tried this also...


// --- test.cpp ---

#include "test.h"

template class Foo<int>;
//void some_friend_function(Foo<int>&);         // ... with this.

当我编译上面的代码时(有或没有注释行),我只得到以下导出符号:

0000000000000000 W _ZN3FooIiE20some_member_functionEi

因此,非模板友元函数肯定不会与类模板的显式实例化一起被实例化(和extern)。这是正常的吗?至少,这就是 GCC 产生的结果(在 4.6.3 和 4.7.2 上测试过)。

有什么方法可以让友元函数标记为外部?我知道这不是一个大问题,因为我可以愉快地接受根据需要实例化的友元函数(即非外部函数),但我很想知道是否有办法做到这一点,如果没有的话, 是疏忽还是故意的?

编辑:明显的解决方法

我的问题具体是关于非模板友元函数,而不是关于寻找解决方法来避免这个问题,这是微不足道的。第一个明显的解决方法是:

template <typename T>
class Foo {
  private:
    T value;
  public:
    template <typename U>
    friend void some_friend_function(Foo<U>& obj) {
      obj.value -= T(42);
    };
};

extern template class Foo<int>;
extern template void some_friend_function(Foo<int>&);

// --- in cpp file: ---

template class Foo<int>;
template void some_friend_function(Foo<int>&);

还有一个匹配的比较紧密但是比较麻烦的是这样的:

template <typename T> class Foo;  // forward-declare.

template <typename T>
void some_friend_function(Foo<T>&);  // declaration.

template <typename T>
class Foo {
  private:
    T value;
  public:
    friend void some_friend_function<>(Foo<T>& obj);  // befriend the T-specialization.
};

template <typename T>
void some_friend_function(Foo<T>& obj) {  // definition.
  obj.value -= T(42);
};

extern template class Foo<int>;
extern template void some_friend_function(Foo<int>&);

// --- in cpp file: ---

template class Foo<int>;
template void some_friend_function(Foo<int>&);

最佳答案

“外部模板类”的作用是声明一个显式实例可用。显式实例化声明的效果不适用于内联函数或模板特化(14.7.2 [temp.explicit] 第 10 段):

Except for inline functions and class template specializations, explicit instantiation declarations have the effect of suppressing the implicit instantiation of the entity to which they refer.

由于类定义中的 friend 函数定义必然是 inline 函数,因此它将保持 inline 函数独立于显式实例化模板的声明(而且,正如您正确指出的那样,它不是模板,也不遵循模板实例化规则)。

关于c++ - 外部模板类和非模板友元函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12877931/

有关c++ - 外部模板类和非模板友元函数的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  3. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  4. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  5. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  6. Ruby——嵌套类和子类是一回事吗? - 2

    下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby​​解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc

  7. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  8. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  9. ruby-on-rails - Mandrill API 模板 - 2

    我正在使用Mandrill的RubyAPIGem并使用以下简单的测试模板:testastic按照Heroku指南中的示例,我有以下Ruby代码:require'mandrill'm=Mandrill::API.newrendered=m.templates.render'test-template',[{:header=>'someheadertext',:main_section=>'Themaincontentblock',:footer=>'asdf'}]mail(:to=>"JaysonLane",:subject=>"TestEmail")do|format|format.h

  10. C51单片机——实现用独立按键控制LED亮灭(调用函数篇) - 2

    说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时

随机推荐