在GCC中,功能模板的section属性被静默忽略 [英] section attribute of a function template is silently ignored in GCC

查看:200
本文介绍了在GCC中,功能模板的section属性被静默忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一组特定的功能放在单独的部分中,并在使用GCC时遇到麻烦.

I'm trying to put a specific set of functions into a separate section and got trouble doing it with GCC.

namespace /* anonymous */ {
  [[gnu::section(".mysection")]]
  void regular_func() { }

  template <class T>
  [[gnu::section(".mysection")]]
  void template_func() { }
} // namespace /* anonymous */

void (*ptr1)() = &regular_func;
void (*ptr2)() = &template_func<int>;

使用clang,regular_functemplate_func<int>的符号都按预期放置在.mysection中.

With clang, the symbols for both of regular_func and template_func<int> are placed in .mysection as I expected.

$ clang++ -std=c++14 a.cpp -c && objdump -t a.o | grep -E "regular|template"
0000000000000000 l     F .mysection 0000000000000006 _ZN12_GLOBAL__N_112regular_funcEv
0000000000000010 l     F .mysection 0000000000000006 _ZN12_GLOBAL__N_113template_funcIiEEvv

但是对于GCC,功能模板不是放置在.mysection中,而是放置在部分中.

But with GCC, the function template is not placed in .mysection, but in the .text.* section.

$ g++ -std=c++14 a.cpp -c && objdump -t a.o | grep -E "regular|template"
0000000000000000 l     F .mysection 0000000000000007 _ZN12_GLOBAL__N_112regular_funcEv
0000000000000000 l     F .text  0000000000000007 _ZN12_GLOBAL__N_113template_funcIiEEvv

我正在使用clang-3.7.1和gcc-5.3.0.

I'm using clang-3.7.1 and gcc-5.3.0.

如何强制gcc将模板实例化的函数放在单独的部分中?

How can I force gcc to put the template-instantiated function in a separate section?

推荐答案

可能不太舒服,但如果您应用section,则GCC会强制执行 属性为template <class T> void template_func()的显式实例化, 对于您要实例化的每个T,例如

It may be small comfort, but GCC will oblige if you apply the section attribute to an explicit instantiation of template <class T> void template_func(), for each T that you want to be instantiated, e.g.

namespace /* anonymous */ {
    [[gnu::section(".mysection")]]
    void regular_func() { }

    template <class T>
    void template_func() { }

    template [[gnu::section(".mysection")]] void template_func<int>();

} // namespace /* anonymous */


void (*ptr1)() = &regular_func;
void (*ptr2)() = &template_func<int>;

然后:

$ g++ -std=c++14 a.cpp -c && objdump -C -t a.o | grep -E "regular|template"
0000000000000000 l     F .mysection 0000000000000007 (anonymous namespace)::regular_func()
0000000000000007 l     F .mysection 0000000000000007 void (anonymous namespace)::template_func<int>()

不幸的是, clang 拒绝:

template [[gnu::section(".mysection")]] void template_func<int>();

说:

template [[gnu::section(".mysection")]] void template_func<int>();
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: an attribute list cannot appear here

因此每个编译器必须通过条件编译使用自己的方式.

so each compiler must have its own way, via conditional compilation.

此外,此修复程序带来了更多的头痛,您必须以某种方式确保 template_func()不能为您未显式定义的任何T实例化 实例化.

Moreover this fix brings the added headache that you must somehow ensure that template_func() can't be instantated for any T that you haven't explicitly instantiated.

您可以通过在函数模板的主体中静态断言以下内容来实现: T是允许实例化的A,B,C...类型之一.那如果 曾经用T = D实例化,则static_assert将触发;你可以 将D添加到列表中,并为D添加显式实例化:

You may achieve that by statically asserting in the function template's body that T is one of types A,B,C... that you allow to be instantiated. Then if it is ever instantiated with T = D, the static_assert will fire; you can add D to the list and add an explicit instantiation for D:

#include <type_traits>

template<typename T, typename First>
constexpr bool is_in()
{
    return std::is_same<T,First>::value;
}

template<typename T, typename First, typename Second, typename ...Rest>
constexpr bool is_in()
{
    return is_in<T,First>() || is_in<T,Second,Rest...>();
}

namespace /* anonymous */ {
    [[gnu::section(".mysection")]]
    void regular_func() { }

    template <class T>
    void template_func() 
    {
        static_assert(is_in<T,int,float>(),"");
    }

    template [[gnu::section(".mysection")]] void template_func<int>();
    template [[gnu::section(".mysection")]] void template_func<float>();

} // namespace /* anonymous */


void (*ptr1)() = &regular_func;
void (*ptr2)() = &template_func<int>;
void (*ptr3)() = &template_func<float>;
void (*ptr4)() = &template_func<char>; // <-- static_assert fails

这篇关于在GCC中,功能模板的section属性被静默忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆