相关疑难解决方法(0)

我必须在何处以及为何要使用"模板"和"typename"关键字?

在模板,在那里,为什么我必须把typenametemplate上依赖的名字呢?究竟什么是依赖名称?我有以下代码:

template <typename T, typename Tail> // Tail will be a UnionNode too.
struct UnionNode : public Tail {
    // ...
    template<typename U> struct inUnion {
        // Q: where to add typename/template here?
        typedef Tail::inUnion<U> dummy; 
    };
    template< > struct inUnion<T> {
    };
};
template <typename T> // For the last node Tn.
struct UnionNode<T, void> {
    // ...
    template<typename U> struct inUnion {
        char fail[ -2 + (sizeof(U)%2) ]; // Cannot be instantiated for any …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++-faq dependent-name typename

1061
推荐指数
8
解决办法
15万
查看次数

模板特化乘法定义符号

我知道我在这里错过了一些简单的东西,但我有一个模板化的成员函数,这是我专门的.

MyClass
{
    template<typename T> T GetTFromVariable(shared_ptr<TOtSimpleVariable> v, string s);
}

template<typename T>
T MyClass::GetTFromVariable(shared_ptr<TOtSimpleVariable> v, string s)
{
    throw std::runtime_error("Don't know how to convert " + ToString(v->GetString()));
}

template<>
int MyClass::GetTFromVariable<int>(shared_ptr<TOtSimpleVariable> v, string s)
{
    return v->GetInteger();
}

template<>
string MyClass::GetTFromVariable<string>(shared_ptr<TOtSimpleVariable> v, string s)
{
    return v->GetString();
}

// etc for other specialisations.
Run Code Online (Sandbox Code Playgroud)

这是在我的头文件中定义的(因为模板应该是)但是当我去编译时我得到了一堆多重定义的符号,代表这样的错误是:

     OtCustomZenith_logic.lib(PtPathOutput.obj) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall MyClass::GetTFromVariable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class boost::shared_ptr<class TOtSimpleVariable>,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??$GetTFromVariable@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@CommandProperties@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VTOtSimpleVariable@@@boost@@V12@@Z) already …
Run Code Online (Sandbox Code Playgroud)

c++ linker templates

22
推荐指数
2
解决办法
1万
查看次数

运算符的部分专业化()

我的一个类声明了一个模板化的函数:

template<class A, class B>
A do_something(const std::vector<B> &data)
Run Code Online (Sandbox Code Playgroud)

我想部分专注于typename A.B是一个实现非常小的接口的类型系列,我们使用了很多,所以我希望我的专业化是通用的B.我怀疑这是双重烦恼,因为typename A它只用作返回类型.

从互联网上,我发现我不能部分专门化一个函数,所以我创建了一个类,如下所示:

template<class A, class B> 
class do_something_implementation {
  public:
    do_something_implementation(const std::vector<B> &data_) {
      data = data_;
    }

  int do_something_implementation<int, B>::operator()() {
    /* Complicated algorithm goes here... */
  }

  double do_something_implementation<double, B>::operator()() {
    /* Different complicated algorithm goes here... */
  }

  private:
      std::vector<B> data;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它(使用Visual Studio 2008)时,编译器崩溃(!),我收到以下错误:

fatal error C1001: An internal error has occurred in the compiler.
Run Code Online (Sandbox Code Playgroud)

我认为这是我的问题,而不是编译器.有没有更好的方式来表达我的目标部分专业化?

c++ templates specialization

4
推荐指数
1
解决办法
648
查看次数