Share c++ source_location implementation

hosuperior

New member
của Source_location trong C ++

#C ++ #Source_Location #Error Xử lý #Debugging #Programming

## Source_location trong C ++ là gì?

Source_Location là một lớp tiện ích C ++ 11 cung cấp thông tin về vị trí mã nguồn của một biểu thức cụ thể.Nó có thể được sử dụng để xử lý lỗi và gỡ lỗi.

## Làm thế nào để sử dụng Source_Location trong C ++?

Để sử dụng Source_Location, bạn có thể chỉ cần bao gồm tệp tiêu đề <iostream> và sau đó sử dụng cú pháp sau:

`` `C ++
std :: Source_Location loc = std :: Source_Location :: current ();
`` `

Điều này sẽ trả về một đối tượng Source_location có chứa thông tin về vị trí mã nguồn của biểu thức được gọi là nó.Đối tượng Source_Location có các thành viên sau:

* File_Name: Tên của tệp nguồn.
* line_number: Số dòng của biểu thức.
* Cột_Number: Số cột của biểu thức.

## Ví dụ về việc sử dụng Source_location trong C ++

Dưới đây là một số ví dụ về cách bạn có thể sử dụng Source_location trong C ++:

* Để in vị trí mã nguồn của một biểu thức cụ thể, bạn có thể sử dụng mã sau:

`` `C ++
std :: cout << "Vị trí mã nguồn của biểu thức này là:" << loc << std :: endl;
`` `

* Để ném một ngoại lệ với thông tin vị trí mã nguồn, bạn có thể sử dụng mã sau:

`` `C ++
Ném std :: runtime_error ("xảy ra lỗi tại" + loc.file_name () + ":" + std :: to_string (loc.line_number ()));
`` `

* Để gỡ lỗi chương trình, bạn có thể sử dụng mã sau để in vị trí mã nguồn của từng biểu thức được thực thi:

`` `C ++
#Define gỡ lỗi

#ifdef gỡ lỗi
#include <Istream>
#endif

int main () {
// ...

#ifdef gỡ lỗi
std :: cout << "Vị trí mã nguồn của biểu thức này là:" << loc << std :: endl;
#endif

// ...

trả lại 0;
}
`` `

## Phần kết luận

Source_Location là một lớp tiện ích hữu ích có thể được sử dụng để xử lý lỗi và gỡ lỗi trong C ++.Nó là một lớp đơn giản để sử dụng và nó có thể cung cấp thông tin có giá trị về vị trí mã nguồn của một biểu thức cụ thể.

## Đọc thêm

* [C ++ 11 Nguồn_Location Tài liệu] (https://en.cppreference.com/w/cpp/error/source_location)
=======================================
of source_location in C++

#C++ #Source_Location #Error handling #Debugging #Programming

## What is source_location in C++?

source_location is a C++11 utility class that provides information about the source code location of a particular expression. It can be used for error handling and debugging.

## How to use source_location in C++?

To use source_location, you can simply include the header file <iostream> and then use the following syntax:

```c++
std::source_location loc = std::source_location::current();
```

This will return a source_location object that contains information about the source code location of the expression that called it. The source_location object has the following members:

* file_name: The name of the source file.
* line_number: The line number of the expression.
* column_number: The column number of the expression.

## Examples of using source_location in C++

Here are some examples of how you can use source_location in C++:

* To print the source code location of a particular expression, you can use the following code:

```c++
std::cout << "The source code location of this expression is: " << loc << std::endl;
```

* To throw an exception with the source code location information, you can use the following code:

```c++
throw std::runtime_error("An error occurred at " + loc.file_name() + ":" + std::to_string(loc.line_number()));
```

* To debug a program, you can use the following code to print the source code location of each expression that is executed:

```c++
#Define DEBUG

#ifdef DEBUG
#include <iostream>
#endif

int main() {
// ...

#ifdef DEBUG
std::cout << "The source code location of this expression is: " << loc << std::endl;
#endif

// ...

return 0;
}
```

## Conclusion

source_location is a useful utility class that can be used for error handling and debugging in C++. It is a simple class to use, and it can provide valuable information about the source code location of a particular expression.

## Further reading

* [C++11 source_location documentation](https://en.cppreference.com/w/cpp/error/source_location)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top