test
Posted blog-yejy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了test相关的知识,希望对你有一定的参考价值。
C++代码
#include <iostream>
#include <functional>
#include <fstream>
#include <ostream>
#include <string>
#include <vector>
#include <algorithm>
#include <initializer_list>
int test()
{
int a = 5;
return a;
}
using namespace std;
void parserTest()
{
string strLine;
}
void fun(char *)
{
std::cout << "foo(char*) is called" << std::endl;
}
void fun(int)
{
std::cout << "foo(int) is called" << std::endl;
}
constexpr int fibonacci ( const int n)
{
return n == 1 || n == 2 ? 1 : fibonacci (n -1) + fibonacci (n -2);
}
class initializer_list_Test
{
public:
initializer_list_Test(std::initializer_list<int> list)
{
for(std::initializer_list<int>::iterator itr = list.begin(); itr != list.end(); itr++)
{
m_vecTest.push_back(*itr);
}
}
std::vector<int> m_vecTest;
};
template<typename T, typename U>
auto add(T a, U b) -> decltype(a + b)
{
return a + b;
}
int main()
{
// std::function
std::function<int(void)> function_Test;
function_Test = nullptr;
try
{
function_Test();
}
catch(std::bad_function_call e)
{
std::cout << e.what() << std::endl;
}
function_Test = test;
std::cout << function_Test()<< std::endl;
// lamda
/** [ captures ] <tparams>(optional)(c++20) ( params ) specifiers exception attr ->
* ret requires(optional)(c++20) { body } (1)
* [ captures ] ( params ) -> ret { body } (2)
* [ captures ] ( params ) { body } (3)
* [ captures ] { body } (4)
*/
int test_param1 = 56;
auto f = [](int test_param1){return test_param1;};
auto test_param2 = [=](int i = 6)mutable noexcept(true)-> int{ i += 12; test_param1 += 1; return test_param1 + i;};
bool bTest = true;
int a = static_cast<int>(bTest);
std::cout << test_param2() << " " << a << std::endl;
// nullptr
if(std::is_same<decltype(NULL), decltype(0)>::value)
std::cout << "null == 0" << std::endl;
if(std::is_same<decltype(NULL), decltype((void*)0)>::value)
std::cout << "null == (void *)0" << std::endl;
if(std::is_same<decltype(NULL), decltype(nullptr)>::value)
std::cout << "null == nullptr" << std::endl;
fun(0);
fun(nullptr);
//fun(NULL);
// constexpr
int arr_i[fibonacci(6) + 1] = {0};
std::cout << arr_i[5] << std::endl;
//if declare param C++ 17
// std::vector<int> vecTest = {1,2,3,4};
// if(std::vector<int>::iterator itr = std::find(vecTest.begin(),vecTest.end(),3); itr != vecTest.end())
// {
// *itr = 10;
// }
// initializer_list 可以作为类construct的形参类型,也可以作为普通函数的形参类型
initializer_list_Test initializer_Test{0,1,2,3,4,5};
for(auto itr = initializer_Test.m_vecTest.begin();
itr != initializer_Test.m_vecTest.end(); ++ itr)
{
std::cout << *itr << std::endl;
}
auto arr = new auto(10);
//类型推导 auto decltype
std::cout <<add<int, double>(5,3.0)<< std::endl;
auto q = add<int, double>(50, 3.12);
if(std::is_same<decltype(q), double>::value)
{
std::cout << "decltype(q) == double" << std::endl;
}
return 0;
}
以上是关于test的主要内容,如果未能解决你的问题,请参考以下文章