C++11特性(1):宏、static_assert、noexcept

#include <iostream>  
#include <string>  
#include <assert.h>  
  
//2、_Pragma(字符串字面量)  
//_Pragma("once") ===== #pragma  once  
//  
  
//3、变长参数宏定义及__VA_ARGS__     
//以省略号作为参数列表的最后一个参数  
#define LOG(...) {\  
    fprintf_s(stderr, "%s: Line %d:\t", __FILE__, __LINE__);\  
    fprintf_s(stderr, __VA_ARGS__);\  
    fprintf_s(stderr, "\n");\  
}  
  
template<typename T, typename U>  
void bit_copy(T &a, U& b)  
{  
    //4、static_assert:静态断言,在编译的时候就进行断言  
    //static_assert(sizeof a == sizeof b, "Two type Must have same width");     compile error  
    memcpy_s(&a, sizeof a, &b, sizeof b);  
}  
  
//4、注意点:由于static_assert是在编译期间进行断言,即static_assert的第一个参数必须是个常量表达式,  
//  如果使用变量,则会导致出错  
void positive(const int n)  
{  
    //使用变量的断言,还是使用assert  
    //static_assert(n > 0, "value must > 0");     error  
    assert(n > 0);  
}  
  
//5、noexcept :用来替代throw()  
//直接在函数声明后带上noexcept关键字或者接受一个常量表达式作为参数,可以转换为bool类型  
//若值为true,则表示函数不抛出异常,反之则抛出异常  
//不带常量表达式的noexcept相当于noexcept(true),即不抛出异常  
//vs好像不支持noexcept  
void Throw(){ throw 1; }  
void NoBlockThrow(){ Throw(); }  
void BlockThrow() throw(){ Throw(); }  
  
int main()  
{  
    //1、__fun__:返回函数名(C++11)--->VS2015  
    //std::cout << __func__ << std::endl;  
    std::cout << __LINE__ << std::endl;//行号  
    std::cout << __FILE__ << std::endl;//文件名  
  
    LOG("x = %d", __LINE__);  
  
    int a = 3;  
    double b = 4.0;  
    bit_copy(a, b);  
  
    try//Throw()  
    {  
        Throw();  
    }  
    catch (...)  
    {  
        std::cerr << "Throw() error" << std::endl;  
    }  
  
    try//NoBlockThrow()  
    {  
        NoBlockThrow();  
    }  
    catch (...)  
    {  
        std::cerr << "NoBlockThrow() error" << std::endl;  
    }  
  
    try//BlockThrow()  
    {  
        BlockThrow();  
    }  
    catch (...)  
    {  
        std::cerr << "BlockThrow() error" << std::endl;  
    }  
  
    return 0;  
}  


====================打个广告,欢迎关注====================

QQ:412425870
csdn博客:
http://blog.youkuaiyun.com/caychen
码云:
https://gitee.com/caychen/
github:
https://github.com/caychen

点击群号或者扫描二维码即可加入QQ群:

328243383(1群)



点击群号或者扫描二维码即可加入QQ群:

180479701(2群)




usr/include/c++/11/bits/basic_string.h:448:34: note: no known conversion for argument 1 from ‘uint64_t’ {aka ‘long unsigned int’} to ‘const std::allocator<char>’ 448 | basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT | ~~~~~~~~~~~~~~^~~ /usr/include/c++/11/bits/basic_string.h:439:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 439 | basic_string() | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:439:7: note: candidate expects 0 arguments, 1 provided In file included from amain.cpp:21: modbus_parser.h:160:21: error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(double&)160 | value = static_cast<T>(double_val); | ^~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/11/string:55, from /usr/include/c++/11/bits/locale_classes.h:40, from /usr/include/c++/11/bits/ios_base.h:41, from /usr/include/c++/11/ios:42, from /usr/include/c++/11/ostream:38, from /usr/include/c++/11/iostream:39, from amain.cpp:1: /usr/include/c++/11/bits/basic_string.h:638:9: note: candidate: ‘template<class _InputIterator, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&) [with _InputIterator = _InputIterator; <template-parameter-2-2> = <template-parameter-1-2>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 638 | basic_string(_InputIterator __beg, _InputIterator __end, | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:638:9: note: template argument deduction/substitution failed: In file included from amain.cpp:21: modbus_parser.h:160:21: note: candidate expects 3 arguments, 1 provided 160 | value = static_cast<T>(double_val); | ^~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/11/string:55, from /usr/include/c++/11/bits/locale_classes.h:40, from /usr/include/c++/11/bits/ios_base.h:41, from /usr/include/c++/11/ios:42, from /usr/include/c++/11/ostream:38, from /usr/include/c++/11/iostream:39, from amain.cpp:1: /usr/include/c++/11/bits/basic_string.h:600:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 600 | basic_string(basic_string&& __str, const _Alloc& __a) | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:600:7: note: candidate expects 2 arguments, 1 provided /usr/include/c++/11/bits/basic_string.h:596:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 596 | basic_string(const basic_string& __str, const _Alloc& __a) | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:596:7: note: candidate expects 2 arguments, 1 provided /usr/include/c++/11/bits/basic_string.h:592:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::initializer_list<_Tp>, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 592 | basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()) | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:592:45: note: no known conversion for argument 1 from ‘double’ to ‘std::initializer_list<char>’ 592 | basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()) | ~~~~~~~~~~~~~~~~~~~~~~~~~^~~ /usr/include/c++/11/bits/basic_string.h:565:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 565 | basic_string(basic_string&& __str) noexcept | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:565:35: note: no known conversion for argument 1 from ‘double’ to ‘std::__cxx11::basic_string<char>&&’ 565 | basic_string(basic_string&& __str) noexcept | ~~~~~~~~~~~~~~~^~~~~ /usr/include/c++/11/bits/basic_string.h:553:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’ 553 | basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:553:7: note: candidate expects 3 arguments, 1 provided /usr/include/c++/11/bits/basic_string.h:533:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>’ 533 | basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()) | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:533:34: note: no known conversion for argument 1 from ‘double’ to ‘const char*’ 533 | basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()) | ~~~~~~~~~~~~~~^~~ /usr/include/c++/11/bits/basic_string.h:518:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’ 518 | basic_string(const _CharT* __s, size_type __n, | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:518:7: note: candidate expects 3 arguments, 1 provided /usr/include/c++/11/bits/basic_string.h:500:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’ 500 | basic_string(const basic_string& __str, size_type __pos, | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:500:7: note: candidate expects 4 arguments, 1 provided /usr/include/c++/11/bits/basic_string.h:484:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’ 484 | basic_string(const basic_string& __str, size_type __pos, | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:484:7: note: candidate expects 3 arguments, 1 provided /usr/include/c++/11/bits/basic_string.h:469:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’ 469 | basic_string(const basic_string& __str, size_type __pos, | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:469:7: note: candidate expects 3 arguments, 1 provided /usr/include/c++/11/bits/basic_string.h:456:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 456 | basic_string(const basic_string& __str) | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:456:40: note: no known conversion for argument 1 from ‘double’ to ‘const std::__cxx11::basic_string<char>&’ 456 | basic_string(const basic_string& __str) | ~~~~~~~~~~~~~~~~~~~~^~~~~ /usr/include/c++/11/bits/basic_string.h:448:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 448 | basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:448:34: note: no known conversion for argument 1 from ‘double’ to ‘const std::allocator<char>&’ 448 | basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT | ~~~~~~~~~~~~~~^~~ /usr/include/c++/11/bits/basic_string.h:439:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 439 | basic_string() | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:439:7: note: candidate expects 0 arguments, 1 provided In file included from amain.cpp:21: modbus_parser.h:185:21: error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(__gnu_cxx::__alloc_traits<std::allocator<short unsigned int>, short unsigned int>::value_type&)185 | value = static_cast<T>(processed_registers[0]); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/11/string:55, from /usr/include/c++/11/bits/locale_classes.h:40, from /usr/include/c++/11/bits/ios_base.h:41, from /usr/include/c++/11/ios:42, from /usr/include/c++/11/ostream:38, from /usr/include/c++/11/iostream:39, from amain.cpp:1: /usr/include/c++/11/bits/basic_string.h:638:9: note: candidate: ‘template<class _InputIterator, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&) [with _InputIterator = _InputIterator; <template-parameter-2-2> = <template-parameter-1-2>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 638 | basic_string(_InputIterator __beg, _InputIterator __end, | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:638:9: note: template argument deduction/substitution failed: In file included from amain.cpp:21: modbus_parser.h:185:21: note: candidate expects 3 arguments, 1 provided 185 | value = static_cast<T>(processed_registers[0]); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/11/string:55, from /usr/include/c++/11/bits/locale_classes.h:40, from /usr/include/c++/11/bits/ios_base.h:41, from /usr/include/c++/11/ios:42, from /usr/include/c++/11/ostream:38, from /usr/include/c++/11/iostream:39, from amain.cpp:1: /usr/include/c++/11/bits/basic_string.h:600:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 600 | basic_string(basic_string&& __str, const _Alloc& __a) | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:600:7: note: candidate expects 2 arguments, 1 provided /usr/include/c++/11/bits/basic_string.h:596:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 596 | basic_string(const basic_string& __str, const _Alloc& __a) | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:596:7: note: candidate expects 2 arguments, 1 provided /usr/include/c++/11/bits/basic_string.h:592:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::initializer_list<_Tp>, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 592 | basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()) | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:592:45: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<short unsigned int>, short unsigned int>::value_type’ {aka ‘short unsigned int’} to ‘std::initializer_list<char>’ 592 | basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()) | ~~~~~~~~~~~~~~~~~~~~~~~~~^~~ /usr/include/c++/11/bits/basic_string.h:565:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ 565 | basic_string(basic_string&& __str) noexcept | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:565:35: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<short unsigned int>, short unsigned int>::value_type’ {aka ‘short unsigned int’} to ‘std::__cxx11::basic_string<char>&&’ 565 | basic_string(basic_string&& __str) noexcept | ~~~~~~~~~~~~~~~^~~~~ /usr/include/c++/11/bits/basic_string.h:553:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’ 553 | basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:553:7: note: candidate expects 3 arguments, 1 provided /usr/include/c++/11/bits/basic_string.h:533:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>’ (near match) 533 | basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()) | ^~~~~~~~~~~~ /usr/include/c++/11/bits/basic_string.h:533:7: note: conversion of argument 1 would be ill-formed: In file included from amain.cpp:21: modbus_parser.h:185:21: error: invalid conversion from ‘__gnu_cxx::__alloc_traits<std::allocator<short unsigned int>, short unsigned int>::value_type’ {aka ‘short unsigned int’} to ‘const char*’ [-fpermissive] 185 | value = static_cast<T>(processed_registers[0]); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | __gnu_cxx::__alloc_traits<std::allocator<short unsigned int>, short unsigned int>::value_type {aka short unsigned int}
最新发布
09-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值