考虑以下代码:
#include <type_traits>
template<typename T>
struct A1 {
T t;
// implicitly-declared default constructor
};
template<typename T>
struct A2 {
T t;
// explicitly-declared default constructor without noexcept
A2() = default;
};
template<typename T>
struct A3 {
T t;
// explicitly-declared default constructor with noexcept
A3() noexcept(std::is_nothrow_default_constructible<T>::value) = default;
};
Run Code Online (Sandbox Code Playgroud)
这三个默认构造函数在 C++ 中等效吗?
几行代码值得千言万语:
我有三个简单的文件:header.h,main.cpp,other.cpp
// header.h
#pragma once
inline const int& GetConst()
{
static int n = 0;
return n;
}
const int& r = GetConst();
// main.cpp
#include "header.h"
int main()
{
return 0;
}
// other.cpp
#include "header.h"
Run Code Online (Sandbox Code Playgroud)
在编译最简单的项目时,VC++ 2010抱怨如下:
ClCompile:
other.cpp
main.cpp
Generating Code...
other.obj : error LNK2005: "int const & const r" (?r@@3ABHB) already defined in main.obj
D:\Test\Debug\bug.exe : fatal error LNK1169: one or more multiply defined symbols found
Build FAILED.
Time Elapsed 00:00:00.29
========== Build: 0 succeeded, …Run Code Online (Sandbox Code Playgroud) class A
{
public:
A()
{
cout << "A()" << endl;
}
A(const A&)
{
cout << "A(const A&)" << endl;
}
A(A&&)
{
cout << "A(A&&)" << endl;
}
A& operator=(const A&)
{
cout << "A(const A&)" << endl;
}
A& operator=(A&&)
{
cout << "A(const A&&)" << endl;
}
~A()
{
cout << "~A()" << endl;
}
};
A&& f_1()
{
A a;
return static_cast<A&&>(a);
}
A f_2()
{
A a;
return static_cast<A&&>(a);
}
int main()
{
cout …Run Code Online (Sandbox Code Playgroud) template<class CharType>
struct StringWithLength
{
size_t length;
CharType* str_buf;
};
Run Code Online (Sandbox Code Playgroud)
我想在字段长度上添加一些注释.我有两个选择:
#1. "The field length is the size of str_buf by the byte"
(Consider "The worker is paid by the hour")
#2. "The field length is the size of str_buf in bytes"
Run Code Online (Sandbox Code Playgroud)
从英语母语人士的角度来看哪个更自然?
谢谢.
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
struct A
{
A(char* p)
: p(p)
{}
~A()
{
delete this->p;
}
char* p;
};
int main()
{
A a(new char);
_CrtDumpMemoryLeaks();
}
Run Code Online (Sandbox Code Playgroud)
在调试模式下运行后,Visual Studio 2012的输出窗口显示:
Detected memory leaks!
Dumping objects ->
{142} normal block at 0x007395A8, 1 bytes long.
Data: < > CD
Object dump complete.
Run Code Online (Sandbox Code Playgroud)
原因是什么?
在Lisp中,可以评估任何表达式.C++采用的概念是:"表达式","价值","评估".
如果您不知道"表达式","值"和"评估"之间的关系,请参阅C++标准5.1.
我知道?:表达式与+表达式相同.
必须能够评估任何表达式并给出值.然而,?:表达似乎并非总是如此.
void f1() {}
void f2() {}
void test(bool b)
{
b ? f1() : f2(); // OK. What's the value of this expression?
}
Run Code Online (Sandbox Code Playgroud)
任何表达式都应该有一个值; b ? f1() : f2();是一种表达; 它的价值是什么?
任何解释?
更新和我自己的答案:
摘自C++标准5.1:
表达式可能会导致值,并可能导致副作用.
我的编译器是VC++ 2013和2013 Novmember CTP.
以下代码使VC++编译器崩溃并报告:
"致命错误C1001:编译器中发生内部错误."
template<class T>
class A
{
operator T*() const
{
return p;
}
T* p;
};
template<class T>
class B : public A<T>
{
using A::operator T*;
};
int main()
{}
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main()
{
FILE* cmd = popen("grep Hello", "w");
fwrite("Hello\n", 6, 6, cmd);
fwrite("Hillo\n", 6, 6, cmd);
fwrite("Hello\n", 6, 6, cmd);
pclose(cmd);
}
Run Code Online (Sandbox Code Playgroud)
上述程序输出:
二进制文件(标准输入)匹配
为什么grep会给出消息,以及如何修复它?
#include <vector>
template
<
typename T,
typename Alloc,
template<typename, typename> class Left
>
Left<T, Alloc>&&
operator <<(Left<T, Alloc>&& coll, T&& value)
{
coll.push_back(std::forward<T>(value));
return std::forward<Left<T, Alloc>>(coll);
}
using namespace std;
int main()
{
vector<int> c1;
c1 << int(8);
}
Run Code Online (Sandbox Code Playgroud)
VS 2015年产量:
错误C2678:二进制'<<':找不到运算符,它采用类型'std :: vector>'的左手操作数(或者没有可接受的转换)
为什么模板模板参数不能按预期工作?
根据cppreferences,explicit runtime_error( const std::string& what_arg );不会复制what_arg的内容.
我可以安全地将临时字符串对象传递给std::runtime_error's ctor?
例如:
std::string GetATempString(const char* msg)
{
return { msg };
}
int main()
{
try {
throw std::runtime_error(GetATempString("Hello"));
} catch (const std::runtime_error& e)
{
e.what(); // Is it guaranteed that "Hello" would be returned safely?
}
}
Run Code Online (Sandbox Code Playgroud) c++ ×9
c++11 ×3
visual-c++ ×3
c ×2
standards ×2
api ×1
class ×1
coding-style ×1
comments ×1
const ×1
evaluation ×1
exception ×1
expression ×1
file ×1
io ×1
linkage ×1
memory-leaks ×1
noexcept ×1
pipe ×1
raii ×1
reference ×1
rvalue ×1
templates ×1