本文介绍了为什么模板不能在外部“C”块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续问题,是否可能typedef指针-to-extern-C-function type in a template?



此代码无法使用 g ++ ,Visual C / C ++和Comeau C / C ++基本上具有相同的错误消息:

  #include< cstdlib> 

externC{
static int do_stuff(int){
return 3;
}

template< typename return_t_,typename arg1_t_>
struct test {
static void foo(return_t_(*)(arg1_t_)){}
};
}

int main()
{
test< int,int> :: foo(& do_stuff);
return EXIT_SUCCESS;
}

g ++说error:template with C linkage,Visual C / C ++ emit编译器错误,Comeau C / C ++说错误:此声明可能没有extern C链接。



事情是,所有人都满意:

  #include< cstdlib> 

externC{
static int do_stuff(int){
return 3;
}

struct test {
static void foo(int(*)(int)){}
};
}

int main()
{
test :: foo(& do_stuff);
return EXIT_SUCCESS;
}

C ++标准的链接规范第7.5节规定:

甚至给出了示例:

  externC {
class X {
void mf(); //函数的名字mf和成员
//函数的类型有C ++语言链接
void mf2(void(*)()); //函数名称mf2有C ++语言
// linkage;该参数具有指向C函数的类型指针
};
}

如果允许模板在externC实例化将有C ++链接。



为什么,C ++ 98标准状态的第14章模板:

这意味着模板可能有链接?什么是模板链接?



为什么明确禁止拥有一个具有C链接的模板,当一个类是好的时,模板的所有成员函数默认构造函数,析构函数和赋值运算符重载)将有C ++链接?

解决方案

所有名称都有外部链接,内部链接或没有链接(C ++ 03§3.5p2 ),但是这不是与语言链接相同的链接。 (对我有用[0]丢个板砖[0]引用|举报|编辑删除管理回复次数: void f(){
struct S {};
矢量< S> v; //不允许,因为S有内部链接。
}

请注意,C ++ 98在您引用的14p4,但C ++ 03删除了可能,因为模板不能在一个上下文中声明它们内部链接:

  void f(){
//不允许:
template< class T>
struct S {};
}


This is a follow-up question to an answer to Is it possible to typedef a pointer-to-extern-"C"-function type within a template?

This code fails to compile with g++, Visual C/C++, and Comeau C/C++ with basically the same error message:

#include <cstdlib>

extern "C" {
    static int do_stuff(int) {
        return 3;
    }

    template <typename return_t_, typename arg1_t_>
    struct test {
        static void foo(return_t_ (*)(arg1_t_)) { }
    };
}

int main()
{
    test<int, int>::foo(&do_stuff);
    return EXIT_SUCCESS;
}

g++ says "error: template with C linkage", Visual C/C++ emits compiler error C2894, and Comeau C/C++ says "error: this declaration may not have extern "C" linkage".

The thing is, all are happy with:

#include <cstdlib>

extern "C" {
    static int do_stuff(int) {
        return 3;
    }

    struct test {
        static void foo(int (*)(int)) { }
    };
}

int main()
{
    test::foo(&do_stuff);
    return EXIT_SUCCESS;
}

Section 7.5, Linkage specifications, of the C++ Standard states:

And it even gives the example:

extern "C" {
    class X {
        void mf(); // the name of the function mf and the member
                // function's type have C++ language linkage
        void mf2(void(*)()); // the name of the function mf2 has C++ language
                // linkage; the parameter has type pointer to C function
    };
}

If templates were allowed in extern "C" blocks, then the member functions of the instantiations would have C++ linkage.

Why, then, does chapter 14, Templates, of the C++98 Standard state:

What does it mean that a template "may" have linkage? What is template linkage?

Why is it explicitly forbidden to have a template with C linkage, when a class is okay, and all member functions of instantiations of the template (the default constructor, destructor, and assignment operator overload) would have C++ linkage?

解决方案

All names either have external linkage, internal linkage, or have no linkage (C++03 §3.5p2), but this is not the same linkage as language linkage. (Confusing, I know. C++0x changes things around considerably with linkage, too.) External linkage is required for anything used as a template argument:

void f() {
  struct S {};
  vector<S> v;  // Not allowed as S has internal linkage.
}

Notice that C++98 has "may" in what you quoted of §14p4, but C++03 removes the "may", as templates cannot be declared in a context that would give them internal linkage:

void f() {
  // Not allowed:
  template<class T>
  struct S {};
}

这篇关于为什么模板不能在外部“C”块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 00:38