小编Bul*_*net的帖子

为什么decltype没有看到成员声明?

试图编译这个简单的类:

#include <vector>
struct M
{
// interface
    auto begin() -> decltype(identities.begin())
    {
        return identities.begin();
    }
// implementation
private:
    std::vector<int> identities;
};
Run Code Online (Sandbox Code Playgroud)

导致错误:

$ g++-510 where.cpp -std=c++11
where.cpp:57:35: error: ‘struct M’ has no member named ‘identities’
     auto begin() ->decltype(this->identities.begin())
                                   ^
where.cpp:57:35: error: ‘struct M’ has no member named ‘identities’

$ clang++ where.cpp -std=c++11 -Wall -pedantic -Wextra
where.cpp:57:35: error: no member named 'identities' in 'M'
    auto begin() ->decltype(this->identities.begin())
                            ~~~~  ^
Run Code Online (Sandbox Code Playgroud)

为什么decltype看不到班级成员?

c++ c++11

27
推荐指数
2
解决办法
1288
查看次数

Clang 和 GCC 对于重载函数模板是否不明确存在分歧

我正在尝试移植一些为 GCC (8.2) 编写的代码以供 Clang 编译:

#include <tuple>

struct Q{};

using TUP = std::tuple<Q>;


template<typename Fn>
inline
void feh(Fn&, const std::tuple<>*)
{}

template<typename Fn, typename H>
inline
void feh(Fn& fn, const std::tuple<H>*)
{
    fn(H{});
}

template<typename  Fn, typename H, typename... R>
inline
void feh(Fn& fn, const std::tuple<H, R...>*)
{
    fn(H{});
    using Rest = const std::tuple<R...>*;
    feh<Fn, R...>(fn, static_cast<Rest>(nullptr));
}

template<typename Tuple, typename Fn>
inline
void fe(Fn& fn, const Tuple  *  tpl =  nullptr)
{
    feh(fn, tpl);
}

int main()
{ …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++11

23
推荐指数
3
解决办法
1701
查看次数

可能在G ++ 6.1.0中回归

以下代码

#include <string>
#include <map>
#include <cassert>

    struct       AV {
        explicit AV(std::string const&) {}
    };

#if 1
    static void check_cache_item(
        std::map<std::string, std::string> const& items) // FIXME remove
    {
        assert(!items.empty());
    }
#endif
    static void check_cache_item(
        std::map<std::string, AV> const& items)
    {
        assert(!items.empty());
    }


int main()
{
    check_cache_item({ { "id", "0" }, { "pk", "#0" } });
    check_cache_item({ { "id", "0" }, { "pk", "#1" } });
    check_cache_item({ { "id", AV{"0"} }, { "pk", AV{"#1"} } });
}
Run Code Online (Sandbox Code Playgroud)

被g ++ 4.8.4,g ++ 5.3.0,clang …

c++ c++11

22
推荐指数
1
解决办法
468
查看次数

将nullptr传递给std :: string :: assign是否有效?

我有一个返回指针和长度的函数,我想调用std::string::assign(pointer, length).clear当长度为零且指针可能为nullptr时,是否必须进行特殊情况(调用)?

C++标准说:

21.4.6.3 basic_string::assign

basic_string& assign(const charT* s, size_type n);
Requires: s points to an array of at least n elements of charT.
Run Code Online (Sandbox Code Playgroud)

那么如果n是零呢?什么是零字符数组,如何指向它?打电话有效吗?

s.assign(nullptr, 0);
Run Code Online (Sandbox Code Playgroud)

还是未定义的行为?

s当大小n为零时,libstdc ++的实现似乎不会取消引用指针,但这几乎不是保证.

c++ c++-standard-library language-lawyer

21
推荐指数
4
解决办法
1857
查看次数

有没有办法使用ON_CALL使模拟函数"有趣"?

鉴于:

#include "gmock/gmock.h"
#include <string>

using namespace testing; // tsk, tsk

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

struct Mockable {
    virtual std::string ify(int x) const;
};

std::string Mockable::ify(int x) const
{
    return std::to_string(x);
}

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

struct Mocked : public Mockable {
    MOCK_CONST_METHOD1(ify, std::string(int)); …
Run Code Online (Sandbox Code Playgroud)

c++ unit-testing gmock

17
推荐指数
1
解决办法
1万
查看次数

eclipse中的Open Resource窗口显示了pyc文件

在eclipse中,打开资源窗口(Hot键到Cmd + Shift + R)通过键入它的名称打开文件似乎显示*.pyc文件,尽管导航器视图正确隐藏它们.

无论如何设置它忽略PYC文件?

我查看了以下链接,看来pydev可能需要在主eclipse文件夹下面的src文件夹.我没有那个结构,但是整个项目在项目属性下被标记为Pydev中的'source'文件夹 - PYTHONPATH.

http://sourceforge.net/projects/pydev/forums/forum/293649/topic/2183420 http://pydev.org/manual_101_project_conf2.html

eclipse pydev

16
推荐指数
1
解决办法
3788
查看次数

std :: string :: compare(const char *)会抛出异常吗?

这是过载(4)在这里

在“异常”部分中,重载2、3、5、6(具有pos1和/或pos2参数)被命名为throwing std::out_of_range

重载(4)没有“ pos”参数,但未标记noexcept

是否抛出该结果取决于实现?

在GCC 7的libstdc ++中,它调用char_traits<char>::lengthchar_traits<char>::compare。这些似乎无法抛出,但没有标记noexcept

c++ language-lawyer

12
推荐指数
1
解决办法
192
查看次数

模板模板参数不匹配

下面的代码

#include <cstdint>

template<typename  T>
struct allo{};

template <typename T,  std::size_t a  = alignof(T)>
struct AA{
    constexpr std::size_t align() { return a; }
};

template<template <typename> typename AT>
struct SAR{
};

using UHR = SAR<allo>;

using AHR = SAR<AA>;


int main()
{
    UHR u;
    AHR a;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

被 GCC 使用 接受-std=c++17,但被 GCC 使用std=c++14和 clang 拒绝(无论 C++ 方言如何)。

https://godbolt.org/z/xaE56Y5Pj

哪个编译器是正确的?这是 C++17 中 clang 尚未实现的一些更改吗?

c++

9
推荐指数
1
解决办法
253
查看次数

GCC 和 clang 对于默认值是与参数同名的函数的参数存在分歧

在工作中,我们有一个长期运行的项目,它使用 GCC (8.2) 成功编译。当我们尝试使用基于 clang 的工具(clang-tidy、静态分析器等)时,我们发现 clang 有时会报告 GCC 接受的代码错误。这是一个最近的例子:

#include <vector>

struct Fox{};

using Foxes = std::vector<Fox>;

Foxes& foxes()
{
    static Foxes f;
    return f;
}

void foxy(Foxes& foxes = foxes())
{
    (void)foxes;
}
Run Code Online (Sandbox Code Playgroud)

上述内容被 GCC(8.2 和 12.1)接受,并带有-Wall -Wpedantic -Wextra -Wshadow -Werror. 然而,clang却抱怨道error: default argument references parameter 'foxes'

https://godbolt.org/z/seEb1ahKP

看起来 clang 认为标志foxes右侧的与左侧的=相同。foxesGCC 似乎认为foxes右侧的=是之前定义的函数。

当 GCC 和 clang 不一致时,通常至少其中之一是错误的(除非代码格式不正确但不需要诊断)。哪一个在这里?

GCC 的行为对我来说似乎很可疑;我本以为至少-Wshadow应该对与全局范围内定义的函数同名的参数(局部变量)发出警告。

c++ gcc clang language-lawyer

7
推荐指数
0
解决办法
60
查看次数

如何防止使用临时调用构造函数

我有一个类,其构造函数采用一些向量并存储它们.

struct X {
    X(std::vector<int> const& ints, std::vector<float> const& floats, std::vector<std::string> const& strings)
    : ints_{ints}
    , floats_{floats}
    , strings_{strings}      
    {};

    std::vector<int> ints_;
    std::vector<float> floats_;
    std::vector<std::string> strings_;
};
Run Code Online (Sandbox Code Playgroud)

我想将成员变量转换为引用,因为在生产代码中,传递给构造函数的值是左值,其生命周期比类X的对象长.

但是,单元测试通常X用临时构造es,如下所示:

X x{ {42}, {3.14f}, {"hello"} };
Run Code Online (Sandbox Code Playgroud)

如果X要引用成员,则应阻止此类调用.这可以通过编写一个构造函数来完成,该构造函数接受rvalue引用并制作它=delete.

X(std::vector<int> && ints, std::vector<float> && floats, std::vector<std::string> && strings) = delete;
Run Code Online (Sandbox Code Playgroud)

如果所有参数都是临时的,这将阻止实例化.不幸的是,它允许通过调用,其中至少有一个参数是左值:

std::vector<std::string> no_strings;
X x{ {42}, {3.14f}, no_strings };
Run Code Online (Sandbox Code Playgroud)

因为左值引用愿意绑定到rvalues,所以(const&,const&,const&)可以调用构造函数.

我是否必须编写lvalue/rvalue ref参数的所有组合(全部七个)并将它们全部标记为已删除?

c++ c++11

6
推荐指数
1
解决办法
267
查看次数