簡體   English   中英

為什么這個模板參數推導在 GCC 而不是 Clang 上失敗?

[英]Why does this template argument deduction fail on GCC but not Clang?

I'm using Clang and GCC trunk with -std=c++20 and the following code compiles fine on Clang but fails on GCC.

#include <cstdint>
#include <climits>
#include <type_traits>
#include <concepts>
#include <immintrin.h>

#define VECTOR_SIZE 32

template <typename T> requires std::is_arithmetic_v<T>
using vec __attribute__((__vector_size__(VECTOR_SIZE))) = T;

template <std::unsigned_integral T>
constexpr vec<T> rotl(vec<T> x, int k)
{
    constexpr auto N = CHAR_BIT * sizeof(T);
    return (x << k) | (x >> (N - k));
}

template <std::unsigned_integral T>
constexpr vec<T> rotr(vec<T> x, int k)
{
    constexpr auto N = CHAR_BIT * sizeof(T);
    return (x >> k) | (x << (N - k));
}

vec<uint32_t> test(vec<uint32_t> x)
{
    return rotr(x, 7);
}

GCC 錯誤:

x86-64 gcc (trunk) (Editor #1, Compiler #1) C++#1 with x86-64 gcc (trunk)
<source>: In function 'vec<unsigned int> test(vec<unsigned int>)':
<source>:27:20: error: no matching function for call to 'rotr(vec<unsigned int>&, int)'
27 |         return rotr(x, 7);
    |                ~~~~^~~~~~
<source>:19:22: note: candidate: 'template<class T>  requires  unsigned_integral<T> constexpr vec<T> rotr(vec<T>, int)'
19 |     constexpr vec<T> rotr(vec<T> x, int k)
    |                      ^~~~
<source>:19:22: note:   template argument deduction/substitution failed:
<source>:19:22: note: constraints not satisfied
In file included from <source>:3:
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts: In substitution of 'template<class T>  requires  unsigned_integral<T> constexpr vec<T> rotr(vec<T>, int) [with T = __vector(8) unsigned int]':
<source>:27:20:   required from here
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts:102:13:   required for the satisfaction of 'integral<_Tp>' [with _Tp = __vector(8) unsigned int]
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts:108:13:   required for the satisfaction of 'unsigned_integral<T>' [with T = __vector(8) unsigned int]
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts:102:24: note: the expression 'is_integral_v<_Tp> [with _Tp = __vector(8) unsigned int]' evaluated to 'false'
102 |     concept integral = is_integral_v<_Tp>;
    |                        ^~~~~~~~~~~~~~~~~~
ASM generation compiler returned: 1
<source>: In function 'vec<unsigned int> test(vec<unsigned int>)':
<source>:27:20: error: no matching function for call to 'rotr(vec<unsigned int>&, int)'
27 |         return rotr(x, 7);
    |                ~~~~^~~~~~
<source>:19:22: note: candidate: 'template<class T>  requires  unsigned_integral<T> constexpr vec<T> rotr(vec<T>, int)'
19 |     constexpr vec<T> rotr(vec<T> x, int k)
    |                      ^~~~
<source>:19:22: note:   template argument deduction/substitution failed:
<source>:19:22: note: constraints not satisfied
In file included from <source>:3:
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts: In substitution of 'template<class T>  requires  unsigned_integral<T> constexpr vec<T> rotr(vec<T>, int) [with T = __vector(8) unsigned int]':
<source>:27:20:   required from here
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts:102:13:   required for the satisfaction of 'integral<_Tp>' [with _Tp = __vector(8) unsigned int]
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts:108:13:   required for the satisfaction of 'unsigned_integral<T>' [with T = __vector(8) unsigned int]
/opt/compiler-explorer/gcc-trunk-20210524/include/c++/12.0.0/concepts:102:24: note: the expression 'is_integral_v<_Tp> [with _Tp = __vector(8) unsigned int]' evaluated to 'false'
102 |     concept integral = is_integral_v<_Tp>;
    |                        ^~~~~~~~~~~~~~~~~~
Execution build compiler returned: 1

我懷疑它是模板參數推導中的 GCC 錯誤,這是由於您的別名與矢量內在屬性相結合而產生的

因此,沒有模板參數推導的代碼本身似乎是完全有效的,只是以某種方式 GCC 最終扣除T = __vector(8) unsigned int ,然后std::unsigned_integral<T>概念失敗,因為is_integral_v<T>評估為false

我想要獲得更好的答案來解釋為什么會發生這種情況,您將需要language-lawyer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM