从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 …
在以下程序中,我在一个线程中有一个虚拟调用:
#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) 为什么以及如何运作?什么类型的'自动'在这里?
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之外的类型是什么?
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
声明来完成吗?
以下程序是否违反严格别名规则?
#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)
在下面的程序试图建立一个由以引用的节点树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) 在以下程序中,如果参数按值传递,如何完成移动?
两个构造函数签名; 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) 这个问题是继上一个问题之后提出的。
以下程序无法编译并报告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) 定义一个类时,以下是否有效?
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只是指复制构造函数,而不是移动构造函数?
我希望我能做到这一点:
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
已经包含在指向成员函数getf
和setf
类型的指针中.
但是,如果我只尝试模板
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++ ×9
constructor ×2
templates ×2
boost ×1
c++11 ×1
c++14 ×1
c++17 ×1
cmake ×1
enum-class ×1
inheritance ×1
lambda ×1
scope ×1
sfinae ×1
type-safety ×1