草庐IT

c++ - 具有模板实例化的模板静态库

coder 2023-11-17 原文

我正在处理一个静态库,该库有多个类模板和函数模板。我知道为了在静态库中使用模板,所有内容(声明/定义)都需要在头文件中。但是,在这种特殊情况下,因为我知道我认为我可以使用前向声明特化的特化类型。

这个技巧与类模板(及其函数)配合得很好,我可以使用我的应用程序代码中的所有库函数。但是,一旦我在库中引入免费函数模板并尝试使用我的应用程序代码中的免费模板函数,它就会给我链接器错误:

error LNK2019: unresolved external symbol "class TemplatedStaticLib __cdecl HelpingRegistration(int)" (??$HelpingRegistration@H@@YA?AV?$TemplatedStaticLib@H@@H@Z) referenced in function _main 1>C:\src\cpp\vs2008\StaticLibExample\MathFuncsLib\Debug\TemplatedStaticLibApp.exe : fatal error LNK1120: 1 unresolved externals" I am using VS2008, here is the code

//静态库头文件(.h)

#ifndef _TEMPLATED_STATIC_LIB_
#define _TEMPLATED_STATIC_LIB_

#include <iostream>

template<typename T>
class TemplatedStaticLib
{
public:
    TemplatedStaticLib(){};
    ~TemplatedStaticLib(){};

    void print(T t);

};

template<typename T>
TemplatedStaticLib<T> HelpingRegistration(T);


#endif

//静态库类文件(.cpp)

#include "TemplatedStaticLib.h"

//Specialization
template class TemplatedStaticLib<double>;
template class TemplatedStaticLib<int>;
template class TemplatedStaticLib<std::string>;


template<typename T>
void TemplatedStaticLib<T>::print(T t)
{
    std::cout << "Templated Print "  << typeid(t).name() << std::endl;
}

void HelpingRegistration(void)
{
}

//Specialization of free function
template<> TemplatedStaticLib<int> HelpingRegistration<int>(int);
template<> TemplatedStaticLib<double> HelpingRegistration<double>(double);
template<> TemplatedStaticLib<std::string> HelpingRegistration<std::string>(std::string);

template<typename T>
TemplatedStaticLib<T> HelpingRegistration(T t)
{
       std::cout << "Function Templated Print "  << typeid(t).name() << std::endl;
       return t;
}

//应用代码

#include "TemplatedStaticLib.h"

int main(int argc, char* argv[])
{
    int anInt = 99;
    TemplatedStaticLib<int> test; 
    test.print(anInt);//works

    double aDouble = 3.9;
    TemplatedStaticLib<double> double_test; 
    double_test.print(aDouble); //works

    std::string aString = "James";

    TemplatedStaticLib<std::string> string_test; 
    string_test.print(aString);//works

    //The following lines gives linker error
    HelpingRegistration(anInt);
    HelpingRegistration(aDouble);
    HelpingRegistration(aString);


    return 0;
}

我不确定为什么会有所不同以及如何解决这个问题。任何帮助表示赞赏。

最佳答案

请注意,这些不是前向声明,而是类模板的显式实例化。这就是允许您将定义放在 .cpp 中的原因。文件并且链接器不会出现 Unresolved 引用错误,只要在其他翻译单元中您只使用这些模板实例化即可。

另一方面,这些:

template<> TemplatedStaticLib<int> HelpingRegistration<int>(int);
template<> TemplatedStaticLib<double> HelpingRegistration<double>(double);
template<> TemplatedStaticLib<std::string> HelpingRegistration<std::string>(std::string);

是函数模板的显式特化声明。相反,您最有可能打算做的是提供显式实例化。这样做的语法如下:

template TemplatedStaticLib<int> HelpingRegistration<>(int);
template TemplatedStaticLib<double> HelpingRegistration<>(double);
template TemplatedStaticLib<std::string> HelpingRegistration<>(std::string);

一旦你解决了这个问题,你就会看到编译器实际上会实例化你的HelpingRegistration<>()函数模板,它也会在这样做时发出错误,因为您正在尝试转换 int (分别为 doublestring )到 TemplatedStaticLib<int> 类型的对象(分别为 TemplatedStaticLib<double>TemplatedStaticLib<string> ),没有提供转换(或者至少没有在您发布的代码中显示):

template<typename T>
TemplatedStaticLib<T> HelpingRegistration(T t)
{
    std::cout << "Function Templated Print "  << typeid(t).name() << std::endl;
    return t; // CANNOT BE CONVERTED!
}

修复此错误(例如通过执行 return TemplateStaticLib<T>(); )将使程序编译链接。

关于c++ - 具有模板实例化的模板静态库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15079578/

有关c++ - 具有模板实例化的模板静态库的更多相关文章

  1. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

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

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

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

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

  4. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  5. ruby 正则表达式 - 如何替换字符串中匹配项的第 n 个实例 - 2

    在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg

  6. ruby-on-rails - Rails - 从另一个模型中创建一个模型的实例 - 2

    我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案

  7. ruby-on-rails - RSpec:避免使用允许接收的任何实例 - 2

    我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_

  8. ruby-on-rails - Rails 3.1 中具有相同形式的多个模型? - 2

    我正在使用Rails3.1并在一个论坛上工作。我有一个名为Topic的模型,每个模型都有许多Post。当用户创建新主题时,他们也应该创建第一个Post。但是,我不确定如何以相同的形式执行此操作。这是我的代码:classTopic:destroyaccepts_nested_attributes_for:postsvalidates_presence_of:titleendclassPost...但这似乎不起作用。有什么想法吗?谢谢! 最佳答案 @Pablo的回答似乎有你需要的一切。但更具体地说...首先改变你View中的这一行对此#

  9. ruby-on-rails - 使用 ruby​​ 将多个实例变量转换为散列的更好方法? - 2

    我收到格式为的回复#我需要将其转换为哈希值(针对活跃商家)。目前我正在遍历变量并执行此操作:response.instance_variables.eachdo|r|my_hash.merge!(r.to_s.delete("@").intern=>response.instance_eval(r.to_s.delete("@")))end这有效,它将生成{:first="charlie",:last=>"kelly"},但它似乎有点hacky和不稳定。有更好的方法吗?编辑:我刚刚意识到我可以使用instance_variable_get作为该等式的第二部分,但这仍然是主要问题。

  10. 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.你能做的最好的事情是:

随机推荐