在库中展示ConstexPR专用模板功能
Exposing constexpr specialized template functions in a library
我有一个带有模板函数的类。其中之一是A constexpr 函数。我想将此类编译为库,并使用其他客户端的专业模板功能。示例:
//in myclass.h
struct myclass{
template<typename Entity>
static constexpr const char* myfnc1();
template<typename Entity>
static std::string myfnc2();
};
//in myclass.cpp
template<> const char* myclass::myfnc1<AnotherClass>() {return "str";}
template<> std::string myclass::myfnc2<AnotherClass2>() {return "str2"; }
template const char* myclass::myfnc1<AnotherClass>();
template std::string myclass::myfnc2<AnotherClass2>();
当我尝试在另一个库中使用myfnc1<AnotherClass>
时,它说它不是定义的,但是我可以使用myfnc2<AnotherClass2>
。当我使用nm
检查libmyClass.so时,我可以看到使用另一个Class2创建的MyFNC2模板,但MyFNC1不是。我知道这是原因,但是无论如何是否有任何使代码工作的方法?
我正在使用G 版本4.4.2。
如果我更改:
template std::string myclass::myfnc2<AnotherClass2>() {return "str2"; }
to
template<> std::string myclass::myfnc2<AnotherClass2>() {return "str2"; }
我可以编译。错字?
> g++ -fPIC -shared x.cpp -O3 -o x.so
> nm x.so | c++filt | grep fnc
结果:
0000000000000680 T char const* myclass::myfnc1<AnotherClass>()
0000000000000690 T std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > myclass::myfnc2<AnotherClass2>()
我不知道您是否真的可以通过代码中的故障来编译。但是我可以通过更改进行编译,并根据预期获得结果。但是我正在使用g++ (GCC) 8.2.1
。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- .cpp和.h文件中的模板专用化声明
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 调用专用模板时出错"no matching function for call to [...]"
- 模板专用化(按容器):value_type
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- OpenMP 慢速专用功能
- C 专用模板功能接收文字字符串
- 在库中展示ConstexPR专用模板功能
- 将过载功能转换为专用功能模板
- 特定模板类型的功能专用化
- 模板专用化和普通旧功能
- 通过C 传递专用功能指针
- C++模板功能专用化
- C++具有继承功能的模板部分专用化
- C++中的专用朋友功能
- 可以是类模板内专用于类模板外的模板功能