如何适当地专注于类模板的功能?

发布于 2025-02-11 00:39:47 字数 1938 浏览 0 评论 0 原文

我有一个类,属性,它具有两个模板参数:属性的类型以及该属性是否是可选的(即可能不存在)。

我编写的代表表示这种逻辑的代码看起来像这样:

#include<type_traits>
#include<optional>
#include<iostream>

template<typename Type, bool Optional>
struct Property {
    std::conditional_t<Optional, std::optional<Type>, Type> value;
    void do_thing() {
        if constexpr (Optional) {
            if(value)
                std::cout << *value << std::endl;
            else
                std::cout << "null" << std::endl;
        } else {
            std::cout << value << std::endl;
        }
    }
};

template<bool Optional>
void Property<std::string, Optional>::do_thing() {
    if constexpr (Optional) {
        if(value)
            std::cout << "\"" << *value << "\"" << std::endl;
        else
            std::cout << "null" << std::endl;
    } else {
        std::cout << "\"" << value << "\"" << std::endl;
    }
}

int main() {
    Property<int, false> prop1{11};
    prop1.do_thing();
    Property<int, true> prop2;
    prop2.do_thing();
    Property<std::string, false> prop3{"Test"};
    prop3.do_thing();
}

我需要的一件事是,如果 type> type do_thing()才能更改行为。 > std :: String (或我需要专业的许多其他类型)。

但是,当我尝试编译此代码时,海湾合作委员会抱怨专业化:

<source>:21:48: error: invalid use of incomplete type 'struct Property<std::__cxx11::basic_string<char>, Optional>'
   21 | void Property<std::string, Optional>::do_thing() {
      |                                                ^
<source>:6:8: note: declaration of 'struct Property<std::__cxx11::basic_string<char>, Optional>'
    6 | struct Property {
      |        ^~~~~~~~
Execution build compiler returned: 1

实施此功能专业化的正确方法是什么?

I have a class, Property, that has two template parameters: the type of the property, and whether or not the property is optional (i.e. might not exist).

The code I've written to represent this logic looks like this:

#include<type_traits>
#include<optional>
#include<iostream>

template<typename Type, bool Optional>
struct Property {
    std::conditional_t<Optional, std::optional<Type>, Type> value;
    void do_thing() {
        if constexpr (Optional) {
            if(value)
                std::cout << *value << std::endl;
            else
                std::cout << "null" << std::endl;
        } else {
            std::cout << value << std::endl;
        }
    }
};

template<bool Optional>
void Property<std::string, Optional>::do_thing() {
    if constexpr (Optional) {
        if(value)
            std::cout << "\"" << *value << "\"" << std::endl;
        else
            std::cout << "null" << std::endl;
    } else {
        std::cout << "\"" << value << "\"" << std::endl;
    }
}

int main() {
    Property<int, false> prop1{11};
    prop1.do_thing();
    Property<int, true> prop2;
    prop2.do_thing();
    Property<std::string, false> prop3{"Test"};
    prop3.do_thing();
}

One of the things I need is I need do_thing() to change in behavior if the Type is std::string (or a number of other types I need to specialize).

But, when I try to compile this code, GCC complains about the specialization:

<source>:21:48: error: invalid use of incomplete type 'struct Property<std::__cxx11::basic_string<char>, Optional>'
   21 | void Property<std::string, Optional>::do_thing() {
      |                                                ^
<source>:6:8: note: declaration of 'struct Property<std::__cxx11::basic_string<char>, Optional>'
    6 | struct Property {
      |        ^~~~~~~~
Execution build compiler returned: 1

What is the proper way to implement this specialization of the function?

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

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

发布评论

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

评论(1

也只是曾经 2025-02-18 00:39:47

实施该功能的专业化的正确方法是什么?

正确的方法是首先要首先专门为所需的模板参数进行部分专业化,如下所示:

//partially specialize the class first 
template<bool Optional>
struct Property<std::string, Optional>
{
    std::conditional_t<Optional, std::optional<std::string>, std::string> value;
    void do_thing();
};
//now write the implementation for the member function
template<bool Optional>
void Property<std::string, Optional>::do_thing() {
    if constexpr (Optional) {
        if(value)
            std::cout << "\"" << *value << "\"" << std::endl;
        else
            std::cout << "null" << std::endl;
    } else {
        std::cout << "\"" << value << "\"" << std::endl;
    }
}

a>

What is the proper way to implement this specialization of the function?

The correct way would be to first partially specialize the class template itself for the required template arguments as shown below:

//partially specialize the class first 
template<bool Optional>
struct Property<std::string, Optional>
{
    std::conditional_t<Optional, std::optional<std::string>, std::string> value;
    void do_thing();
};
//now write the implementation for the member function
template<bool Optional>
void Property<std::string, Optional>::do_thing() {
    if constexpr (Optional) {
        if(value)
            std::cout << "\"" << *value << "\"" << std::endl;
        else
            std::cout << "null" << std::endl;
    } else {
        std::cout << "\"" << value << "\"" << std::endl;
    }
}

Working Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文