不同c++编译器之间的自动类型推导不匹配
Mismatched deduction of auto types between different c++ compilers
所以,我正在尝试实现点积(https://en.wikipedia.org/wiki/Dot_product)在现代C++的一些风格中,并提出了以下代码:
#include <iostream>
template<class... Args>
auto dot(Args... args)
{
auto a = [args...](Args...)
{
return [=](auto... brgs)
{
static_assert(sizeof...(args) == sizeof...(brgs));
auto v1 = {args...}, i1 = v1.begin();
auto v2 = {brgs...}, i2 = v2.begin();
typename std::common_type<Args...>::type s = 0;
while( i1 != v1.end() && i2!= v2.end())
{
s += *i1++ * *i2++;
}
return s;
};
};
return a(std::forward<Args>(args)...);
}
int main()
{
auto a = dot(1,3,-5)(4,-2,-1);
std::cout << a << std::endl;
}
在线:https://gcc.godbolt.org/z/kDSney以及:cppnights
上面的代码用g++
编译并执行得很好,但clang
(以及icc
和msvc
(会阻塞它:
clang++ ./funcpp.cpp --std=c++17
./funcpp.cpp:12:4: error: 'auto' deduced as 'std::initializer_list<int>' in declaration of
'v1' and deduced as 'const int *' in declaration of 'i1'
auto v1 = {args...}, i1 = v1.begin();
^ ~~~~~~~~~ ~~~~~~~~~~
./funcpp.cpp:28:11: note: in instantiation of function template specialization
'dot<int, int, int>' requested here
auto a = dot(1,3,-5)(4,-2,-1);
^
1 error generated.
现在,如果我把v1
、v2
、i1
、i2
的定义分解为:
auto v1 = {args...} ;
auto i1 = v1.begin();
auto v2 = {brgs...};
auto i2 = v2.begin();
clang
和msvc
没有问题,icc
仍然阻塞:
<source>(10): error: static assertion failed
static_assert(sizeof...(args) == sizeof...(brgs));
^
detected during instantiation of "auto dot(Args...) [with Args=<int, int, int>]" at line 30
compilation aborted for <source> (code 2)
Execution build compiler returned: 2
但是,如果我删除了有问题的static_assert
,那么icc
编译代码也没有问题。
除了(典型的(问题:哪一个是正确的,为什么:(具体的问题是:
根据[dcl.spec.auto]
:
如果在每次推导中替换占位符类型的类型不相同,则程序是格式错误的
clang
正确地确定了有问题的行中定义了两种不同的类型:'auto' deduced as 'std::initializer_list<int>' in declaration of 'v1' and deduced as 'const int *' in declaration of 'i1'
,所以我想听听你的意见是否:
- 考虑到这种特定情况,我是否遇到了一些未记录的g++扩展(在https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/C_002b_002b-Extensions.html#C_002b_002b-扩展(,因为据我所知g++正确地处理了自动声明列表中的不同类型
- 或者g++没有推断出这两种类型是不同的(…嗯…(
- 还是别的什么
感谢您通读这个长问题。(作为奖励,如果有人能回答icc
在static_assert
上失败的原因,那就太好了。(
从我的评论扩展:
g++并不总是这样做,考虑一下auto i = 0l, f = 0.0;
的例子,它给出了错误:
test.cpp: In function ‘int main()’:
test.cpp:4:5: error: inconsistent deduction for ‘auto’: ‘long int’ and then ‘double’
4 | auto i = 0l, f = 0.0;
如果我们编译您的程序并打印变量的类型(使用这种方法(,我们会得到以下输出:
v1: std::initializer_list<int>, i1: int const*
v2: std::initializer_list<int>, i2: int const*
使用gcc 9.2.0版本,带有标志-std=c++17 -pedantic -Wall -Wextra
,没有任何警告或错误。
根据您对标准的评论,该程序格式不正确,标准规定,除非另有规定(在本例中没有(,否则应发出诊断消息(警告或错误(。因此,我认为这是gcc中的一个bug。
这是一个已知的错误。
ICC上的static_assert
失败肯定是一个错误。我找到了一个简单的解决方法,将static_assert
移动到一个单独的函数中。不是很优雅的解决方案,但它有效。
经过轻微修改,这是用GCC、Clang和ICC编译的代码:
template<std::size_t size, class... Args>
void args_no_guard(Args... args)
{
static_assert(sizeof...(args) == size);
}
template<class... Args>
auto dot(Args... args)
{
return [=](auto... brgs)
{
constexpr auto n = sizeof...(args);
args_no_guard<n>(brgs...);
using T = std::common_type_t<decltype(args)..., decltype(brgs)...>;
const T v1[]{static_cast<T>(args)...};
const T v2[]{static_cast<T>(brgs)...};
T dot = 0;
for (std::size_t i = 0; i < n; ++i)
dot += v1[i] * v2[i];
return dot;
};
}
- 将 unordered_map 与 Catch2 谓词一起使用时类型不匹配
- 重载函数的地址与所需类型不匹配
- 参数错误可能与类型不匹配有关?
- 调用子例程时类型不匹配
- POSIX(C )参数类型不匹配
- typedef 类型不匹配,将其传递到函数中
- 为什么此代码编译 (C++11) 而没有类型不匹配错误
- 候选模板被忽略:推断的类型与调整后的类型不匹配
- 内联 ASM:'out'的操作数类型不匹配
- 为什么我们有一个类型不匹配
- C++类和freeglut(参数类型不匹配)
- 警告 639:二进制操作中类型 'uint64' 的强类型不匹配
- 警告 634:相等或有条件的强类型不匹配(类型"bool")
- 不了解C++类型不匹配:const Foo* to Foo* const&
- 函数声明和定义的返回类型不匹配,编译器可以吗?
- 类型不匹配
- 返回指向常量getter的指针时,返回值类型与函数类型不匹配
- llvm pass replaceAllUses与类型不匹配?
- 参数类型不匹配(无效*)
- 重载运算符中的类型不匹配(写入管道)