关于c++:C14新特性的所有知识点全在这儿啦

3次阅读

共计 4707 个字符,预计需要花费 12 分钟才能阅读完成。

后面程序喵介绍过 C ++11 的新个性,在这里(),这篇文章介绍下 C ++14 的新个性。

函数返回值类型推导

C++14 对函数返回类型推导规定做了优化,先看一段代码:

#include <iostream>

using namespace std;

auto func(int i) {return i;}

int main() {cout << func(4) << endl;
    return 0;
}

应用 C ++11 编译:

~/test$ g++ test.cc -std=c++11
test.cc:5:16: error:‘func’function uses‘auto’type specifier without trailing return type
 auto func(int i) {
                ^
test.cc:5:16: note: deduced return type only available with -std=c++14 or -std=gnu++14

下面的代码应用 C ++11 是不能通过编译的,通过编译器输入的信息也能够看见这个个性须要到 C ++14 才被反对。

返回值类型推导也能够用在模板中:

#include <iostream>
using namespace std;

template<typename T> auto func(T t) {return t;}

int main() {cout << func(4) << endl;
    cout << func(3.4) << endl;
    return 0;
}

留神

)函数内如果有多个 return 语句,它们必须返回雷同的类型,否则编译失败。

auto func(bool flag) {if (flag) return 1;
    else return 2.3; // error
}
// inconsistent deduction for auto return type:‘int’and then‘double’

)如果 return 语句返回初始化列表,返回值类型推导也会失败

auto func() {return {1, 2, 3}; // error returning initializer list
}

) 如果函数是虚函数,不能应用返回值类型推导

struct A {
    // error: virtual function cannot have deduced return type
    virtual auto func() { return 1;} 
}

)返回类型推导能够用在前向申明中,然而在应用它们之前,翻译单元中必须可能失去函数定义

auto f();               // declared, not yet defined
auto f() { return 42;} // defined, return type is int

int main() {cout << f() << endl;
}

)返回类型推导能够用在递归函数中,然而递归调用必须以至多一个返回语句作为先导,以便编译器推导出返回类型。

auto sum(int i) {if (i == 1)
        return i;              // return int
    else
        return sum(i - 1) + i; // ok
}

lambda 参数 auto

在 C ++11 中,lambda 表达式参数须要应用具体的类型申明:

auto f = [] (int a) {return a;}

在 C ++14 中,对此进行优化,lambda 表达式参数能够间接是 auto:

auto f = [] (auto a) {return a;};
cout << f(1) << endl;
cout << f(2.3f) << endl;

变量模板

C++14 反对变量模板:

template<class T>
constexpr T pi = T(3.1415926535897932385L);

int main() {
    cout << pi<int> << endl; // 3
    cout << pi<double> << endl; // 3.14159
    return 0;
}

别名模板

C++14 也反对别名模板:

template<typename T, typename U>
struct A {
    T t;
    U u;
};

template<typename T>
using B = A<T, int>;

int main() {
    B<double> b;
    b.t = 10;
    b.u = 20;
    cout << b.t << endl;
    cout << b.u << endl;
    return 0;
}

constexpr 的限度

C++14 相较于 C ++11 对 constexpr 缩小了一些限度:

)C++11 中 constexpr 函数能够应用递归,在 C ++14 中能够应用局部变量和循环

constexpr int factorial(int n) { // C++14 和 C++11 均可
    return n <= 1 ? 1 : (n * factorial(n - 1));
}

在 C ++14 中能够这样做:

constexpr int factorial(int n) { // C++11 中不可,C++14 中能够
    int ret = 0;
    for (int i = 0; i < n; ++i) {ret += i;}
    return ret;
}

)C++11 中 constexpr 函数必须必须把所有货色都放在一个独自的 return 语句中,而 constexpr 则无此限度:

constexpr int func(bool flag) { // C++14 和 C++11 均可
    return 0;
}

在 C ++14 中能够这样:

constexpr int func(bool flag) { // C++11 中不可,C++14 中能够
    if (flag) return 1;
    else return 0;
}

[[deprecated]] 标记

C++14 中减少了 deprecated 标记,润饰类、变、函数等,当程序中应用到了被其润饰的代码时,编译时被产生正告,用户提醒开发者该标记润饰的内容未来可能会被抛弃,尽量不要应用。

struct [[deprecated]] A { };

int main() {
    A a;
    return 0;
}

当编译时,会呈现如下正告:

~/test$ g++ test.cc -std=c++14
test.cc: In function‘int main()’:
test.cc:11:7: warning:‘A’is deprecated [-Wdeprecated-declarations]
     A a;
       ^
test.cc:6:23: note: declared here
 struct [[deprecated]] A {

二进制字面量与整形字面量分隔符

C++14 引入了二进制字面量,也引入了分隔符,避免看起来眼花哈~

int a = 0b0001'0011'1010;
double b = 3.14'1234'1234'1234;

std::make_unique

咱们都晓得 C ++11 中有 std::make_shared,却没有 std::make_unique,在 C ++14 曾经改善。

struct A {};
std::unique_ptr<A> ptr = std::make_unique<A>();

std::shared_timed_mutex 与 std::shared_lock

C++14 通过 std::shared_timed_mutex 和 std::shared_lock 来实现读写锁,保障多个线程能够同时读,然而写线程必须独立运行,写操作不能够同时和读操作一起进行。

实现形式如下:

struct ThreadSafe {
    mutable std::shared_timed_mutex mutex_;
    int value_;

    ThreadSafe() {value_ = 0;}

    int get() const {std::shared_lock<std::shared_timed_mutex> loc(mutex_);
        return value_;
    }

    void increase() {std::unique_lock<std::shared_timed_mutex> lock(mutex_);
        value_ += 1;
    }
};

为什么是 timed 的锁呢,因为能够带超时工夫,具体能够自行查问相干材料哈,网上有很多。

std::integer_sequence

template<typename T, T... ints>
void print_sequence(std::integer_sequence<T, ints...> int_seq)
{std::cout << "The sequence of size" << int_seq.size() << ":";
    ((std::cout << ints << ' '), ...);
    std::cout << '\n';
}

int main() {print_sequence(std::integer_sequence<int, 9, 2, 5, 1, 9, 1, 6>{});
    return 0;
}

输入:7 9 2 5 1 9 1 6

std::integer_sequence 和 std::tuple 的配合应用:

template <std::size_t... Is, typename F, typename T>
auto map_filter_tuple(F f, T& t) {return std::make_tuple(f(std::get<Is>(t))...);
}

template <std::size_t... Is, typename F, typename T>
auto map_filter_tuple(std::index_sequence<Is...>, F f, T& t) {return std::make_tuple(f(std::get<Is>(t))...);
}

template <typename S, typename F, typename T>
auto map_filter_tuple(F&& f, T& t) {return map_filter_tuple(S{}, std::forward<F>(f), t);
}

std::exchange

间接看代码吧:

int main() {
    std::vector<int> v;
    std::exchange(v, {1,2,3,4});
    cout << v.size() << endl;
    for (int a : v) {cout << a << " ";}
    return 0;
}

看样子貌似和 std::swap 作用雷同,那它俩有什么区别呢?

能够看下 exchange 的实现:

template<class T, class U = T>
constexpr T exchange(T& obj, U&& new_value) {T old_value = std::move(obj);
    obj = std::forward<U>(new_value);
    return old_value;
}

能够看见 new_value 的值给了 obj,而没有对 new_value 赋值,这里置信您曾经晓得了它和 swap 的区别了吧!

std::quoted

C++14 引入 std::quoted 用于给字符串增加双引号,间接看代码:

int main() {
    string str = "hello world";
    cout << str << endl;
    cout << std::quoted(str) << endl;
    return 0;
}

编译 & 输入:

~/test$ g++ test.cc -std=c++14
~/test$ ./a.out
hello world
"hello world"

对于 C ++14,大略就引入了这些新个性,下期预报:C++17 新个性,请继续关注哈!欢送星标在看点赞与转发~

参考链接

https://en.cppreference.com/w…

https://en.cppreference.com/w…

https://en.cppreference.com/w…

https://en.cppreference.com/w…

https://en.cppreference.com/w…
更多文章,请关注我的 V X 公 主 号:程序喵小孩儿,欢送交换。

正文完
 0