【问题标题】:Solve this Error ,while using 'export' keyword in templates?在模板中使用“export”关键字时解决此错误?
【发布时间】:2010-07-28 11:56:00
【问题描述】:

程序:main.cpp

 struct X {
            int x;
           };

export template <class T> T const& min(T const&, T const&);

int main() 
 {
  return min(2, 3);
 } 

x.cpp

 struct X {
    int  x; 
     };

 export template <class T> T const& min(T const &a, T const &b) {
 return a<b ? a : b;
  } 

错误:使用 gcc 编译

 export.cpp:23: warning: keyword ‘export’ not implemented, and will be ignored
 export.cpp: In function ‘int main()’:
 export.cpp:27: error: call of overloaded ‘min(int, int)’ is ambiguous

 swap.cpp:16: warning: keyword ‘export’ not implemented, and will be ignored

错误:使用 EDG 编译器编译

export.cpp", line 27: error: more than one instance of overloaded function    
export.cpp", line 23: error: support for exported templates is disabled
swap.cpp", line 16: error: support for exported templates is disabled

谁能解决这个问题?

有人解释一下export关键字的用法吗?

【问题讨论】:

  • 我也在使用 EDG 编译器。可以说如何在 EDG 中使用
  • 是 - 不要 - 这样做没有意义。

标签: c++ templates export


【解决方案1】:

export 关键字毫无用处,据我所知,EDG 是唯一实现它的编译器。该关键字在 C++0x 中已弃用。至于它的用法 - 甚至不要考虑它。

【讨论】:

  • +1,EDG 是唯一支持它的编译器,他们推动将其从下一个标准中删除。这更像是一个问题而不是一个解决方案。
  • C++0x 中不弃用 export 关键字;它保留供将来使用。它以前的含义没有被弃用;它已被完全移除 - 对于标准来说,这是一个不寻常的单级移除。
  • @Charles 我觉得一个 SO CW 问题出现在“建​​议 export 关键字的新用途”:-)
【解决方案2】:

看起来您的编译器不支持单独的模板编译。通常的做法是不使用模板单独编译并将模板分发到头文件中。除此之外,我还发现了几个问题。

  1. 从声明中删除 export 关键字。这应该注意调用重载的“min(int, int)”是不明确的错误消息。模板可以定义为在程序中只导出一次。
  2. X 定义了两次。为什么?

附:我从未见过任何使用导出模板的代码。前段时间在学习C++的时候,我试过的每一个编译器都不支持导出模板。难怪它会被 C++ 弃用。

【讨论】:

    【解决方案3】:

    export 关键字已经是 discussed here on SO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 2022-06-12
      • 1970-01-01
      相关资源
      最近更新 更多