小编wal*_*lly的帖子

Visual Studio 2017,Boost和CMake的版本号

从Boost邮件列表中我了解到VS2017具有以下我们可能最感兴趣的版本号:

Visual Studio           15.0
cl; C/C++ Compiler      19.10
Platform Toolset:       v141
Run Code Online (Sandbox Code Playgroud)

Visual Studio 2017 IDE中定义了以下宏:

CrtSDKReferenceVersion  14.0
MSBuildToolsVersion     15.0
PlatformToolsetVersion  141
VCToolsVersion          14.10.25017
VisualStudioVersion     15.0
Run Code Online (Sandbox Code Playgroud)

在编译期间,以下变量是 #define'd:

_MSC_VER                1910
_MSC_FULL_VER           191025017
Run Code Online (Sandbox Code Playgroud)

cl.exe 包含在具有 VC工具版本的MSVC文件夹中.完整的x64文件夹路径是

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX64\x64
Run Code Online (Sandbox Code Playgroud)

cl /Bv 从命令行列表:

Compiler Passes:
 cl.exe:        Version 19.10.25017.0
 c1.dll:        Version 19.10.25017.0
 c1xx.dll:      Version 19.10.25017.0
 c2.dll:        Version 19.10.25017.0
 link.exe:      Version 14.10.25017.0
 mspdb140.dll:  Version 14.10.25017.0
 1033\clui.dll: Version 19.10.25017.0
Run Code Online (Sandbox Code Playgroud)

请注意mspdb140.dll,并link.exe与14.10.25017.0版本上市.


这里似乎msvc …

c++ boost cmake visual-studio-2017

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

线程内的虚拟调用忽略派生类

在以下程序中,我在一个线程中有一个虚拟调用:

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>

class A {
public:
    virtual ~A() { t.join(); }
    virtual void getname() { std::cout << "I am A.\n"; }
    void printname() 
    { 
        std::unique_lock<std::mutex> lock{mtx};
        cv.wait(lock, [this]() {return ready_to_print; });
        getname(); 
    };
    void set_ready() { std::lock_guard<std::mutex> lock{mtx}; ready_to_print = true; cv.notify_one(); }
    void go() { t = std::thread{&A::printname,this}; };

    bool ready_to_print{false};
    std::condition_variable cv;
    std::mutex mtx;
    std::thread t{&A::printname,this};
};

class B : public A {
public:
    int x{4};
};

class C …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading thread-safety

7
推荐指数
1
解决办法
273
查看次数

为什么lambda表达式可以返回本地枚举类类型?

为什么以及如何运作?什么类型的'自动'在这里?

auto lambda = [](){
    enum class Local { X=0 };
    return Local::X;
};

auto x = lambda(); // No error! Why and what type is auto in this case?
auto y = Local::X; // Error! Of course!
Run Code Online (Sandbox Code Playgroud)

enum class Local不拉姆达类型以外公知的.它是一个enum class,因此不能没有int演员阵容,AFAIK.如何返回本地类型auto以及它在lambda之外的类型是什么?

lambda scope type-safety enum-class c++14

7
推荐指数
2
解决办法
289
查看次数

私有使用基础构造函数的声明不是私有的

using基础构造函数的声明是私有的,但仍然可以构造该类.为什么?

可访问性对于必须公开operator[]using声明的工作方式不同.

#include <vector>

template<typename T>
class Vec : std::vector<T>
{
private:
    using std::vector<T>::vector;       // Works, even if private. Why?
public:
    using std::vector<T>::operator[];   // must be public
};

int main(){
    Vec<int> vec = {2, 2};
    auto test = vec[1];
}
Run Code Online (Sandbox Code Playgroud)

如果我希望构造函数是私有的,该怎么办?可以用using声明来完成吗?

c++ inheritance constructor using-declaration

7
推荐指数
1
解决办法
196
查看次数

严格别名违规

以下程序是否违反严格别名规则?

#include <cstdint>

int main()
{
    double d = 0.1;

    //std::int64_t n = *reinterpret_cast<std::int64_t*>(&d); // aliasing violation

    //auto n{*reinterpret_cast<std::int64_t*>(&d)}; // aliasing violation

    auto nptr{reinterpret_cast<std::int64_t*>(&d)};
    auto& n{*nptr};

    ++n;
}
Run Code Online (Sandbox Code Playgroud)

VS2015,clanggcc没有发出警告.

c++ strict-aliasing

6
推荐指数
2
解决办法
292
查看次数

初始化的树元素引用了对deque成员的引用会导致nullptr

下面的程序试图建立一个由以引用的节点树std::deque的元素.

#include <deque>

struct Node;
using Pool = std::deque<Node>;

struct Node
{
    Node(int d, Pool& pool)
        : level{d}
        , l{d > 0 ? pool.emplace_back(d - 1, pool) : *this}
        , r{d > 0 ? pool.emplace_back(d - 1, pool) : *this}
    {
    }

    int level;
    const Node& l;
    const Node& r;

    int check() const
    {
        if(!(&l == this))
            return l.check() + 1 + r.check();
        else
            return 1;
    }
};

int main()
{
    int depth{2};
    Pool pool;
    Node …
Run Code Online (Sandbox Code Playgroud)

c++ c++17

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

即使按值传递,也可以移动值

在以下程序中,如果参数按值传递,如何完成移动?

两个构造函数签名; MyContainer(Myclass&& myclass)并且MyContainer(Myclass myclass)工作同样好,但Myclass只有一个移动构造函数,其余的都被删除.

#include <iostream>

class Myclass {
public:
    Myclass(int value) : value{value} {std::cout << "Constructed with value " << value << "\n";}
    Myclass(Myclass&& other) : value{other.value}
    {
        other.value = 0;
        std::cout << "Move constructed with value " << value << "\n";
    }
    Myclass(const Myclass& other) = delete;
    Myclass& operator=(const Myclass& other) = delete;
    Myclass& operator=(Myclass&& other) = delete;
    ~Myclass() {std::cout << "Destructed with value " << value << "\n";}
private:
    int value; …
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
1
解决办法
117
查看次数

模板默认类型与默认值

这个问题是继上一个问题之后提出的。


情况一:默认类型

以下程序无法编译并报告error C2995: 'T foo(void)': function template has already been defined

#include <iostream>
#include <type_traits>

template < typename T, typename = std::enable_if_t< std::is_integral<T>::value> >
T foo() { std::cout << "integral" << std::endl; return T(); }

template < typename T, typename = std::enable_if_t< !std::is_integral<T>::value> >
T foo() { std::cout << "non-integral" << std::endl; return T(); }

int main() {
    foo<int>();
    foo<float>();
}
Run Code Online (Sandbox Code Playgroud)

每个模板都由两个实例交替foo使用和忽略 (SFINAE) 。所以我假设编译器在某个时候会看到:

template < typename T, typename = void > …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae

5
推荐指数
1
解决办法
1206
查看次数

采用const参数的默认移动构造函数

定义一个类时,以下是否有效?

T(const T&&) = default;
Run Code Online (Sandbox Code Playgroud)

我在这里阅读有关移动构造函数的内容,它解释了如何隐式声明默认值:

一个类可以有多个移动构造函数,例如,T::T(const T&&)T::T(T&&).如果存在一些用户定义的移动构造函数,则用户仍可以使用关键字default强制生成隐式声明的移动构造函数.

在页面底部,它提到了缺陷报告CWG 2171:

CWG 2171 C++ 14
X(const X&&) = default是非常重要的,是微不足道的.

也许wiki条目只是有一个错误,CWG 2171只是指复制构造函数,而不是移动构造函数?

c++ constructor language-lawyer c++11

5
推荐指数
1
解决办法
145
查看次数

从非类型模板参数确定类型

我希望我能做到这一点:

template <typename T x>
struct Test {
    T val{x};
};

int main() {
    Test<3> test;
    return test.val;
}
Run Code Online (Sandbox Code Playgroud)

但我不能.对?


我在这里回答了一个问题,我使用以下模板:

template <typename T, typename V, typename VP, V(T::*getf)(), void (T::*setf)(VP)>
Run Code Online (Sandbox Code Playgroud)

每种类型都是手动指定的.但这是一个重复因为T,V并且VP已经包含在指向成员函数getfsetf类型的指针中.


但是,如果我只尝试模板

template <V(T::*getf)(), void (T::*setf)(VP)>
Run Code Online (Sandbox Code Playgroud)

要么

template <V(T::*getf)(), void (T::*setf)(VP), typename T, typename V, typename VP>
Run Code Online (Sandbox Code Playgroud)

那么类型无法确定.


接下来我尝试了专业化:

template <typename T, typename T2>
struct Accessor;

template <typename V, typename T, typename VP>
struct Accessor <V(T::*)(), void …
Run Code Online (Sandbox Code Playgroud)

c++ templates member-function-pointers

4
推荐指数
1
解决办法
255
查看次数