admin管理员组文章数量:1399297
The C++ program below compiles with GCC trunk:
template <typename T>
struct Foo
{
constexpr void test();
};
void not_constexpr() {}
template <>
constexpr void Foo<int>::test() { }
template <>
void Foo<float>::test() { not_constexpr(); }
int main() {}
But fails with Clang and MSVC. The error message is:
<source>:13:28: error: non-constexpr declaration of 'test' follows constexpr declaration
13 | void Foo<float>::test() { not_constexpr(); }
| ^
<source>:4:18: note: previous declaration is here
4 | constexpr void test();
This can be seen on Compiler Explorer here. Which compiler is correct?
The C++ program below compiles with GCC trunk:
template <typename T>
struct Foo
{
constexpr void test();
};
void not_constexpr() {}
template <>
constexpr void Foo<int>::test() { }
template <>
void Foo<float>::test() { not_constexpr(); }
int main() {}
But fails with Clang and MSVC. The error message is:
<source>:13:28: error: non-constexpr declaration of 'test' follows constexpr declaration
13 | void Foo<float>::test() { not_constexpr(); }
| ^
<source>:4:18: note: previous declaration is here
4 | constexpr void test();
This can be seen on Compiler Explorer here. Which compiler is correct?
Share Improve this question asked Mar 26 at 10:56 user2023370user2023370 11.1k6 gold badges58 silver badges91 bronze badges 1- also rejected by msvc – Oersted Commented Mar 26 at 11:16
1 Answer
Reset to default 5GCC is correct: inline
, constexpr
, constinit
, consteval
, attributes, and contracts are independent for explicit specializations ([temp.expl.spec]/12). The situation is complicated by the special feature of specializing a member of a class template rather than a function template directly (which Clang supports), but that doesn’t make the previous declaration any more controlling.
本文标签: cNonconstexpr specialisation declaration following constexpr declarationStack Overflow
版权声明:本文标题:c++ - Non-constexpr specialisation declaration following constexpr declaration - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744149838a2593011.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论