模板专业化:非内联函数定义问题

发布于 2024-09-16 18:09:32 字数 1628 浏览 3 评论 0原文

以下代码可以正确编译。

#include <string>

template <typename T, typename U>
class Container
{
private:
    T value1;
    U value2;
public:
    Container(){}
    void doSomething(T val1, U val2);
};

template<typename T, typename U>
void Container<typename T, typename U>::doSomething(T val1, U val2)
{
    ; // Some implementation
}

template <>
class Container<char, std::string>
{
private:
    char value1;
    std::string value2;
public:
    Container(){}
    void doSomething(char val1, std::string val2)
    {
        ; // Some other implementation
    }
};

但是,如果我尝试在外部定义 void doSomething(char val1, std::string val2) ,则会出现以下错误。

#include <string>

template <typename T, typename U>
class Container
{
private:
    T value1;
    U value2;
public:
    Container(){}
    void doSomething(T val1, U val2);
};

template<typename T, typename U>
void Container<typename T, typename U>::doSomething(T val1, U val2)
{
    ; // Some implementation
}

template <>
class Container<char, std::string>
{
private:
    char value1;
    std::string value2;
public:
    Container(){}
    void doSomething(char val1, std::string val2);
};

template<>
void Container<char,std::string>::doSomething(char val1, std::string val2)
{
    ; // Some other implementation
}

错误:

错误 1 ​​错误 C2910: '容器::doSomething' : 不能明确 专门的 c:\users\bharani\documents\visual 工作室 2005\项目\模板\模板 专业化\templatespecializationtest.cpp 35

我犯了什么错误?

谢谢。

The following code compiles properly.

#include <string>

template <typename T, typename U>
class Container
{
private:
    T value1;
    U value2;
public:
    Container(){}
    void doSomething(T val1, U val2);
};

template<typename T, typename U>
void Container<typename T, typename U>::doSomething(T val1, U val2)
{
    ; // Some implementation
}

template <>
class Container<char, std::string>
{
private:
    char value1;
    std::string value2;
public:
    Container(){}
    void doSomething(char val1, std::string val2)
    {
        ; // Some other implementation
    }
};

But if I try to define void doSomething(char val1, std::string val2) outside, I get the following error.

#include <string>

template <typename T, typename U>
class Container
{
private:
    T value1;
    U value2;
public:
    Container(){}
    void doSomething(T val1, U val2);
};

template<typename T, typename U>
void Container<typename T, typename U>::doSomething(T val1, U val2)
{
    ; // Some implementation
}

template <>
class Container<char, std::string>
{
private:
    char value1;
    std::string value2;
public:
    Container(){}
    void doSomething(char val1, std::string val2);
};

template<>
void Container<char,std::string>::doSomething(char val1, std::string val2)
{
    ; // Some other implementation
}

Error:

Error 1 error C2910:
'Container::doSomething'
: cannot be explicitly
specialized c:\users\bharani\documents\visual
studio
2005\projects\templates\template
specialization\templatespecializationtest.cpp 35

What mistake do I commit?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2024-09-23 18:09:32

您没有明确专门化成员函数。但是您正在定义显式(类模板)专业化的成员函数。这是不同的,您需要像这样定义它

inline void Container<char,std::string>::doSomething(char val1, std::string val2)
{
    ; // Some other implementation
}

请注意,“内联”很重要,因为这不是模板,并且如果它是在类外部定义的,则它不是隐式内联的。如果将标头包含到多个翻译单元中,则需要内联以避免重复的链接器符号。

您的显式专业化中是否有模板,则必须使用您的语法:

template <>
class Container<char, std::string>
{
private:
    char value1;
    std::string value2;
public:
    Container(){}

    template<typename T, typename U>
    void doSomething(T val1, U val2) { /* primary definition */ }
};

template<>
inline void Container<char,std::string>::doSomething(char val1, std::string val2)
{
    ; // Some other implementation
}

您的第一个代码中也有错误。您需要像这样定义类外定义,类模板的参数列表中没有“typename”

template<typename T, typename U>
void Container<T, U>::doSomething(T val1, U val2) 
{
    ; // Some implementation
}

You are not explicitly specializing a member function. But you are defining the member function of an explicit (class template-) specialization. That's different, and you need to define it like

inline void Container<char,std::string>::doSomething(char val1, std::string val2)
{
    ; // Some other implementation
}

Note that the "inline" is important, because this is not a template and it is not implicitly inline if it is defined outside the class. It's needed to be inline to avoid duplicate linker symbols if you include the header into more than one translation unit.

Would you have a template in your explicit specialization, your syntax would have to be used:

template <>
class Container<char, std::string>
{
private:
    char value1;
    std::string value2;
public:
    Container(){}

    template<typename T, typename U>
    void doSomething(T val1, U val2) { /* primary definition */ }
};

template<>
inline void Container<char,std::string>::doSomething(char val1, std::string val2)
{
    ; // Some other implementation
}

You also have an error in your first code. You need to define the out of class definition like this, without "typename" in the argument list of the class template

template<typename T, typename U>
void Container<T, U>::doSomething(T val1, U val2) 
{
    ; // Some implementation
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文