不同C ++编译器之间的自动类型推导不匹配

因此,我试图以某种现代C ++的形式实现点积(https://en.wikipedia.org/wiki/Dot_product),并提出了以下代码:

#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以及:cppinsights

上面的代码可以用g++编译并很好地执行,但是clang(以及iccmsvc)却使它窒息:

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,-1);
                 ^
1 error generated.

现在,如果我分解v1v2i1i2的定义,例如:

auto v1 = {args...} ;
auto i1 = v1.begin();
auto v2 = {brgs...};
auto i2 = v2.begin();

clangmsvc没问题,icc仍然令人窒息:

<source>(10): error: static assertion failed

                static_assert(sizeof...(args) == sizeof...(brgs));

                ^

          detected during instantiation of "auto dot(Args...) [with Args=<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',所以我想听听您的意见是否:

感谢您阅读这个冗长的问题。 (如果有人能够回答为什么iccstatic_assert上失败的原因,这是很好的选择。)

xukuan007 回答:不同C ++编译器之间的自动类型推导不匹配

根据我的评论展开:

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;

如果我们编译您的程序并打印变量类型(with this method),则会得到以下输出:

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,没有任何警告或错误。

根据您对标准的评论,该程序格式不正确,并且标准specifies应当发出诊断消息(警告或错误),除非另有说明(本例中不是)。因此,我会说这是gcc中的错误。

它是a known bug

,

IC卡上的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;
    };
}
本文链接:https://www.f2er.com/3103067.html

大家都在问