带有字符串模板参数的模板 class 的部分特化
Partial specialiszation of a template class with string template argument
#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<6, name> 并以此错误结束:
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
What is the proper way to specialise template classes with string template arguments?
给定的代码是 well-formed(在 C++20 中)但无法为 gcc 10.2 及更低版本编译。然而,它从 gcc 10.3 及更高版本编译得很好。 Demo
可能是因为并非所有 C++20 功能都在 gcc 10.2 及更低版本中完全实现。
#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<6, name> 并以此错误结束:
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
What is the proper way to specialise template classes with string template arguments?
给定的代码是 well-formed(在 C++20 中)但无法为 gcc 10.2 及更低版本编译。然而,它从 gcc 10.3 及更高版本编译得很好。 Demo
可能是因为并非所有 C++20 功能都在 gcc 10.2 及更低版本中完全实现。