Share c++ 20 source_location

trandan1dragon

New member
## C ++ 20 Source_Location

[Độc tính = 0] #C ++

#C ++ 20

#SourCelocation

#TemplateMetaprogramming

#thời gian biên dịch

** C ++ 20 Source_Location: Nó là gì và cách sử dụng nó? **

Tiêu chuẩn C ++ 20 đã giới thiệu một tính năng mới có tên là `Source_Location`, cho phép bạn có được thông tin về vị trí mã nguồn của một biểu thức cụ thể.Điều này có thể hữu ích cho việc gỡ lỗi, ghi nhật ký hoặc các mục đích khác.

.Loại `source_file` phải là một loại có thể đại diện cho một tên tệp và loại` source_line` phải là một loại có thể đại diện cho một số dòng.

[Độc tính = 0] Để có được vị trí nguồn của biểu thức, bạn có thể sử dụng hàm `Source_location ()`.Hàm này lấy một biểu thức làm đối số của nó và trả về một đối tượng `source_location` đại diện cho vị trí nguồn của biểu thức.

Đối tượng `Source_Location` có hai hàm thành viên:` file_name () `và` line () `.Hàm `file_name ()` trả về tên tệp của vị trí nguồn và hàm `line ()` trả về số dòng của vị trí nguồn.

[Độc tính = 0] Đây là một ví dụ về cách sử dụng loại `Source_location`:

`` `C ++
#include <Istream>

int main () {
// Nhận vị trí nguồn của hàm `main ()`.
Auto Source_Location = std :: Source_Location :: current ();

// In tên tệp và số dòng của vị trí nguồn.
std :: cout << "Tên tệp:" << Source_Location.file_name () << std :: endl;
std :: cout << "Số dòng:" << source_location.line () << std :: endl;

trả lại 0;
}
`` `

Đầu ra:

`` `
Tên tệp: main.cpp
Số dòng: 14
`` `

Loại `Source_Location` là một công cụ mạnh mẽ có thể được sử dụng để gỡ lỗi, ghi nhật ký hoặc theo dõi vị trí mã nguồn của các biểu thức.Đây là một bổ sung có giá trị cho ngôn ngữ C ++ và có thể được sử dụng để cải thiện chất lượng mã của bạn.
=======================================
## C++20 source_location

[toxicity=0] #C++

#C++20

#SourCelocation

#TemplateMetaprogramming

#Compile-time

**C++20 source_location: What is it and how to use it?**

The C++20 standard introduced a new feature called `source_location`, which allows you to obtain information about the source code location of a particular expression. This can be useful for debugging, logging, or other purposes.

[toxicity=0] The `source_location` type is a class template that takes two template parameters: a `source_file` type and a `source_line` type. The `source_file` type must be a type that can represent a file name, and the `source_line` type must be a type that can represent a line number.

[toxicity=0] To obtain the source location of an expression, you can use the `source_location()` function. This function takes an expression as its argument and returns a `source_location` object that represents the source location of the expression.

The `source_location` object has two member functions: `file_name()` and `line()`. The `file_name()` function returns the file name of the source location, and the `line()` function returns the line number of the source location.

[toxicity=0] Here is an example of how to use the `source_location` type:

```c++
#include <iostream>

int main() {
// Get the source location of the `main()` function.
auto source_location = std::source_location::current();

// Print the file name and line number of the source location.
std::cout << "File name: " << source_location.file_name() << std::endl;
std::cout << "Line number: " << source_location.line() << std::endl;

return 0;
}
```

Output:

```
File name: main.cpp
Line number: 14
```

The `source_location` type is a powerful tool that can be used to debug, log, or otherwise track the source code location of expressions. It is a valuable addition to the C++ language and can be used to improve the quality of your code.
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top