c++ - 为什么用 Visual Studio 2013 而不是 g++-4.8.1 编译?

标签 c++ visual-studio c++11 g++ visual-studio-2013

以下示例 ( ideone ) 在 Windows 7 上使用 Visual Studio 2013 时编译并工作,但在 Ubuntu 13.10 上使用 g++4.8.1 时则不能。

#include <cassert>
#include <cstdlib>

#include <array>
#include <iostream>
#include <numeric>
#include <utility>

// Wraps a std::array of TKey/TValue pairs and provides a method
// to randomly select a TKey with TValue bias.
template< typename TKey, typename TValue, std::size_t TSize >
class weights final
{
    public:
        using pair = const std::pair< const TKey, const TValue >;
        using array = const std::array< pair, TSize >;

        weights( array values )
            : values_{ values }
            , sum_{ std::accumulate(values_.begin(), values_.end(), 0, [](TValue total, const pair& p){ return total + p.second; }) }
        {}

        // Implements this algorithm
        // http://stackoverflow.com/a/1761646/331024
        const TKey get() const
        {
            // The real code uses c++11 <random> features,
            // which I've removed for brevity.
            auto weight_rand = static_cast< TValue >( std::rand() % sum_ );

            for ( std::size_t i = 0; i < TSize; ++i )
            {
                if (weight_rand < values_[i].second)
                {
                    return values_[i].first;
                }
                weight_rand -= values_[i].second;
            }
            assert(false);
        }

    private:
        array values_;
        const TValue sum_;
};

enum class direction
{
    NORTH,
    SOUTH,
    EAST,
    WEST
};

// For convenience create a type to map the above
// four-value enumeration to integer weights.
using w4i = weights< direction, int, 4 >;

// Map the directions with a weight.
static const w4i direction_weights = w4i::array{
    {
        w4i::pair{ direction::NORTH, 2 },
        w4i::pair{ direction::EAST, 1 },
        w4i::pair{ direction::SOUTH, 3 },
        w4i::pair{ direction::WEST, 1 }
    }
};

int main()
{
    std::cout << (int)direction_weights.get() << std::endl;    

    return 0;
}

Visual Studio 2013 可以编译运行代码。 g++-4.8.1编译失败,输出如下错误:

$ g++ -std=c++11 -Wall -Wextra -pedantic weights.cpp -o weights
weights.cpp: In instantiation of ‘weights<TKey, TValue, TSize>::weights(array) [with TKey = direction; TValue = int; long unsigned int TSize = 4ul; weights<TKey, TValue, TSize>::array = const std::array<const std::pair<const direction, const int>, 4ul>]’:
weights.cpp:67:5:   required from here
weights.cpp:20:131: error: could not convert ‘values’ from ‘weights<direction, int, 4ul>::array {aka const std::array<const std::pair<const direction, const int>, 4ul>}’ to ‘const std::pair<const direction, const int>’, sum_{ std::accumulate(values_.begin(), values_.end(), 0, [](TValue total, const pair& p){ return total + p.second; }) }

我如何修复/修改它以适用于两种编译器?

最佳答案

您的问题是尝试使用通用初始化,这会引入歧义。使用您的代码并创建最小示例会导致:

不起作用:

struct weights
{
    weights(std::array<int, 1> values)
        : values_{values}
    {}

    std::array<int, 1> values_;
};

作品:

struct weights
{
    weights(std::array<int, 1> values)
        : values_(values)
    {}

    std::array<int, 1> values_;
};

问题是关于什么{values}变得模棱两可应该做。它应该创建一个包含一个元素( values )的初始化列表吗?或者它应该充当通用初始化程序/大括号初始化程序并具有与括号相同的行为?看起来 GCC 和 clang 正在做第一个(导致类型不匹配),而 Visual Studio 正在做第二个(正确的类型检查)。

如果您希望它对两者都有效,请使用括号。

我不知道这是 GCC/clang 或 Visual Studio 中的错误,还是标准中的歧义。

编辑:

举个更简单的例子,考虑一下:

std::array<int, 2> a = {1, 2};
std::array<int, 2> b{a}; // error: no viable conversion from 'std::array<int, 2>' to 'value_type' (aka 'int')
std::array<int, 2> b(a); // works

首先是用一个 std::array<int, 2> 创建一个初始化列表其中的对象,而第二个正确调用复制构造函数。

关于c++ - 为什么用 Visual Studio 2013 而不是 g++-4.8.1 编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22495624/

相关文章:

visual-studio - 如何设置外部防火墙以启用 Visual Studio 远程调试

c++ - 创建索引数组的标准库函数,其对应值为给定数字

c++ - 你如何在 Windows 上为 MinGW 编译 WebkitGTK

c++ - HKEY 句柄为 NULL,在调试期间它写入未使用的<无法读取内存>

c# - 在 azure 中发布网站时,未发布已编辑的页面

visual-studio - 检查您的网络连接或代理设置

c++ - 在此上下文中的完美转发和 std::move 行为

c++ - 面试位: Giving run time error on submit but correct output on custom test case

c++ - C++ 类中的类

C++动态数组和文件读取不能一起工作