support bazel complie this project and format code.
This commit is contained in:
8
basic_content/decltype/BUILD
Normal file
8
basic_content/decltype/BUILD
Normal file
@@ -0,0 +1,8 @@
|
||||
# please run `bazel run basic_content/decltype:decltype`
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "decltype",
|
||||
srcs = ["decltype.cpp"],
|
||||
copts = ["-std=c++11"]
|
||||
)
|
Binary file not shown.
@@ -1,11 +1,3 @@
|
||||
/**
|
||||
* @file decltype.cpp
|
||||
* @brief g++ -o decltype decltype.cpp -std=c++11
|
||||
* @author 光城
|
||||
* @version v1
|
||||
* @date 2019-08-08
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
@@ -14,47 +6,42 @@ using namespace std;
|
||||
*/
|
||||
template <typename T>
|
||||
|
||||
auto multiply(T x, T y)->decltype(x*y)
|
||||
{
|
||||
return x*y;
|
||||
auto multiply(T x, T y) -> decltype(x * y) {
|
||||
return x * y;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int nums[] = {1,2,3,4};
|
||||
vector<int> vec(nums,nums+4);
|
||||
vector<int>::iterator it;
|
||||
int main() {
|
||||
int nums[] = {1, 2, 3, 4};
|
||||
vector<int> vec(nums, nums + 4);
|
||||
vector<int>::iterator it;
|
||||
|
||||
for(it=vec.begin();it!=vec.end();it++)
|
||||
cout<<*it<<" ";
|
||||
cout<<endl;
|
||||
for (it = vec.begin(); it != vec.end(); it++)
|
||||
cout << *it << " ";
|
||||
cout << endl;
|
||||
|
||||
using nullptr_t = decltype(nullptr);
|
||||
nullptr_t nu;
|
||||
int *p = NULL;
|
||||
if (p == nu)
|
||||
cout << "NULL" << endl;
|
||||
|
||||
using nullptr_t = decltype(nullptr);
|
||||
nullptr_t nu;
|
||||
int * p =NULL;
|
||||
if(p==nu)
|
||||
cout<<"NULL"<<endl;
|
||||
typedef decltype(vec.begin()) vectype;
|
||||
|
||||
for (vectype i = vec.begin(); i != vec.end(); i++)
|
||||
cout << *i << " ";
|
||||
cout << endl;
|
||||
|
||||
typedef decltype(vec.begin()) vectype;
|
||||
/**
|
||||
* 匿名结构体
|
||||
*/
|
||||
struct {
|
||||
int d;
|
||||
double b;
|
||||
} anon_s;
|
||||
|
||||
for(vectype i=vec.begin();i!=vec.end();i++)
|
||||
cout<<*i<<" ";
|
||||
cout<<endl;
|
||||
decltype(anon_s) as{1, 2.0}; // 定义了一个上面匿名的结构体
|
||||
|
||||
/**
|
||||
* 匿名结构体
|
||||
*/
|
||||
struct
|
||||
{
|
||||
int d ;
|
||||
double b;
|
||||
}anon_s;
|
||||
cout << multiply(11, 2) << ":" << as.b << endl;
|
||||
|
||||
decltype(anon_s) as; // 定义了一个上面匿名的结构体
|
||||
|
||||
cout<<multiply(11,2)<<endl;
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user