C++ 为什么使用Visual Studio 2013而不是g++-4.8.1?

C++ 为什么使用Visual Studio 2013而不是g++-4.8.1?,c++,visual-studio,c++11,g++,visual-studio-2013,C++,Visual Studio,C++11,G++,Visual Studio 2013,以下示例()在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 TKe

以下示例()在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;
}
#包括
#包括
#包括
#包括
#包括
#包括
//包装TKey/TValue对的std::数组并提供一个方法
//随机选择带有TValue bias的TKey。
模板
班级重量决赛
{
公众:
使用pair=const std::pair;
使用array=const std::array;
权重(数组值)
:值{values}
,sum_{std::accumulate(value_u.begin(),value_u.end(),0,[](TValue-total,const-pair&p){return-total+p.second;})
{}
//实现这个算法
// http://stackoverflow.com/a/1761646/331024
常量TKey get()常量
{
//真正的代码使用c++11特性,
//为了简洁起见,我把它去掉了。
自动重量=静态重量(标准::兰德()%sum);
对于(标准::尺寸i=0;i;
//用砝码标出方向。
静态常数w4i方向_权重=w4i::数组{
{
w4i::对{方向::北,2},
w4i::对{方向::东,1},
w4i::对{方向::南,3},
w4i::对{方向::西,1}
}
};
int main()
{

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

不起作用:

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_;
};

第一个是创建一个初始值设定项列表,其中包含一个
std::array
对象,而第二个是正确调用复制构造函数。

这不是
std::array

struct array {
  int i;
};

int main()
{
  array a;
  array b{a};
}

G++和Clang都拒绝了这一点,因为标准要求他们查看在我报告

Fwiw后打开的,它也在Clang 3.4上呕吐(相同/类似错误)。将
values\uu
成员类型更改为
const-array
,或者在
使用array
decl中丢失
const
,任何一个都可以。@WhozCraig感谢您的叮当声反馈。我现在的内容与您建议的更改之间有什么功能上的区别?这是一些奇特的const-corrective…还有一对额外的
const
以防万一…@DavidRodríguez dribeas
const
应该是所有东西的默认值!:)看着这些代码,我可以看到一些错过的
const
机会,这让我很紧张!我问@WhozCraig是合法的:
使用array=const…
是否意味着数组成员变量也是
const
?g++4.7.2给出的
数组必须在权重构造函数中用括号括起的初始值设定项进行初始化。(
sum{…
line)这看起来像是一个库问题。相同的语法适用于std::vector。@n.m.:我已经提交了一个和一个。@n.m.
vector
不是一个聚合,所以应用了不同的规则。我很确定这是DR1467,VC++是错误所在one@JonathanWakely:那么我应该关闭我的错误报告吗?
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
struct array {
  int i;
};

int main()
{
  array a;
  array b{a};
}