The string literal constant is no longer allowed to be assigned to a
char *. If you need to assign and initialize a
char * with a string literal constant, you should use
const char * or auto.
如果混用C和C++,应对C代码做额外处理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// foo.h #ifdef __cplusplus extern"C" { #endif
intadd(int x, int y);
#ifdef __cplusplus } #endif
// foo.c intadd(int x, int y) { return x+y; }
并在编译时先单独编译C代码,再链接到cpp代码上。
Chapter 2
C++ does not allow to implicitly convert
void * to other types. The type of nullptr is
nullptr_t, which can be implicitly converted to any pointer
or member pointer type, and can be compared equally or unequally with
them.
Starting with C++14, the constexpr function can use simple statements
such as local variables, loops, and branches internally.
intmain(){ auto [x, y, z] = f(); std::cout << x << ", " << y << ", " << z << std::endl; return0; }
类型推导
1
auto arr = newauto(10); // arr as int *
C++20甚至支持函数参数用auto
1 2 3 4 5 6 7
intadd(auto x, auto y){ return x+y; }
auto i = 5; // type int auto j = 6; // type int std::cout << add(i, j) << std::endl;
auto不能用于数组类型的推导
1
auto auto_arr2[10] = {arr}; // illegal, can't infer array type
使用std::is_same<decltype(x), int>::value判断类型是否相同
c++11模板函数返回类型的推导需要如下
1 2 3 4
template<typename T, typename U> autoadd2(T x, U y) -> decltype(x+y){ return x + y; }
C++14则可以用auto直接推导。
1 2 3 4
template<typename T, typename U> autoadd3(T x, U y){ return x + y; }
In simple terms, decltype(auto) is mainly used to derive
the return type of a forwarding function or package, which does not
require us to explicitly specify the parameter expression of
decltype
intfoo(int a, int b, int c){ ; } intmain(){ // bind parameter 1, 2 on function foo, // and use std::placeholders::_1 as placeholder for the first parameter. auto bindFoo = std::bind(foo, std::placeholders::_1, 1,2); // when call bindFoo, we only need one param left bindFoo(1); }
// 遍历 for(int i = 0; i != tuple_len(new_tuple); ++i) // runtime indexing tuple_index(new_tuple, i);
Chapter 5
std::shared_ptr
use_count得到reference count
reset取消该指针的reference,不影响其他指针。
std::weak_ptr has no implemented * and
-> operators
the expired() method of a std::weak_ptr
returns false when the resource is not released
The lock() method returns a std::shared_ptr
to the original object when the resource is not released, or
nullptr otherwise.
Chapter 6
std::regex
1 2 3 4 5 6 7 8 9 10 11 12 13 14
std::string fnames[] = {"foo.txt", "bar.txt", "test", "a0.txt", "AAA.txt"}; std::regex txt_regex("[a-z]+\\.txt"); std::regex_match(fname, txt_regex); // 返回值为bool std::smatch base_match; // std::match_results<std::string::const_iterator> if (std::regex_match(fname, base_match, txt_regex)) { // the first element of std::smatch matches the entire string // the second element of std::smatch matches the first expression // with brackets if (base_match.size() == 2) { std::string base = base_match[1].str(); std::cout << "sub-match[0]: " << base_match[0].str() << std::endl; std::cout << fname << " sub-match[1]: " << base << std::endl; } }