使用字符串模板参数的模板类的部分专业化

发布于 2025-01-31 15:32:14 字数 1755 浏览 2 评论 0原文

#include <iostream>

template<unsigned N>
struct FixedString 
{
    char buf[N + 1]{};
    constexpr FixedString(const char (&s)[N]) 
    {
        for (unsigned i = 0; i != N; ++i)
            buf[i] = s[i];
    }
};

template<int, FixedString name>
class Foo 
{
public:
    auto hello() const { return name.buf; }
};

template<FixedString name>
class Foo<6, name>
{
public:
    auto hello() const { return name.buf; }
};

int main() 
{
    Foo<6, "Hello!"> foo;
    foo.hello();
}

我正在尝试添加一个模板专业foo&lt; 6,name&gt;它以此错误结束:

macros.h: At global scope:
macros.h:3:18: error: class template argument deduction failed:
 23 | class Foo<6, name>
      |                  ^
macros.h:3:18: error: no matching function for call to ‘FixedString(FixedString<...auto...>)’
macros.h:7:15: note: candidate: ‘template<unsigned int N> FixedString(const char (&)[N])-> FixedString<N>’
 7 |     constexpr FixedString(const char (&s)[N])
      |               ^~~~~~~~~~~
macros.h:7:15: note:   template argument deduction/substitution failed:
macros.h:13:18: note:   mismatched types ‘const char [N]’ and ‘FixedString<...auto...>’
 23 | class Foo<6, name>
      |                  ^
macros.h:4:8: note: candidate: ‘template<unsigned int N> FixedString(FixedString<N>)-> FixedString<N>’
 4 | struct FixedString
      |        ^~~~~~~~~~~
macros.h:4:8: note:   template argument deduction/substitution failed:
macros.h:13:18: note:   mismatched types ‘FixedString<N>’ and ‘FixedString<...auto...>’
 23 | class Foo<6, name>

使用字符串模板参数专业的模板类的正确方法是什么? G ++(Ubuntu 9.4.0-1ubuntu1〜16.04)9.4.0

#include <iostream>

template<unsigned N>
struct FixedString 
{
    char buf[N + 1]{};
    constexpr FixedString(const char (&s)[N]) 
    {
        for (unsigned i = 0; i != N; ++i)
            buf[i] = s[i];
    }
};

template<int, FixedString name>
class Foo 
{
public:
    auto hello() const { return name.buf; }
};

template<FixedString name>
class Foo<6, name>
{
public:
    auto hello() const { return name.buf; }
};

int main() 
{
    Foo<6, "Hello!"> foo;
    foo.hello();
}

I'm trying to add a template specialisation Foo<6, name> and it ends in this error:

macros.h: At global scope:
macros.h:3:18: error: class template argument deduction failed:
 23 | class Foo<6, name>
      |                  ^
macros.h:3:18: error: no matching function for call to ‘FixedString(FixedString<...auto...>)’
macros.h:7:15: note: candidate: ‘template<unsigned int N> FixedString(const char (&)[N])-> FixedString<N>’
 7 |     constexpr FixedString(const char (&s)[N])
      |               ^~~~~~~~~~~
macros.h:7:15: note:   template argument deduction/substitution failed:
macros.h:13:18: note:   mismatched types ‘const char [N]’ and ‘FixedString<...auto...>’
 23 | class Foo<6, name>
      |                  ^
macros.h:4:8: note: candidate: ‘template<unsigned int N> FixedString(FixedString<N>)-> FixedString<N>’
 4 | struct FixedString
      |        ^~~~~~~~~~~
macros.h:4:8: note:   template argument deduction/substitution failed:
macros.h:13:18: note:   mismatched types ‘FixedString<N>’ and ‘FixedString<...auto...>’
 23 | class Foo<6, name>

What is the proper way to specialise template classes with string template arguments?
g++ (Ubuntu 9.4.0-1ubuntu1~16.04) 9.4.0

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

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

发布评论

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

评论(1

雪落纷纷 2025-02-07 15:32:14

使用字符串模板参数专业化模板类的正确方法是什么?

给定的代码形成良好(在C ++ 20中),但对于GCC 10.2且较低的编译。然而,它可以从GCC 10.3及更高的GCC中汇总。 demo

可能是由于并非所有C ++ 20个功能都在GCC 10.2中完全实现,并且较低。

What is the proper way to specialise template classes with string template arguments?

The given code is well-formed(in C++20) but fails to compile for gcc 10.2 and lower. It however compiles fine from gcc 10.3 and higher. Demo

Might be due to that not all C++20 features were fully implemented in gcc 10.2 and lower.

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