This attribute marks the function as considering only its numeric parameters. This is mainly intended for the compiler to optimize away repeated calls to a function that the compiler knows will return the same value repeatedly. It applies mostly to math functions that have no static state or side effects, and whose return is solely determined by the inputs.
In this highly-contrived example, the compiler normally must call the square() function in every loop even though we know that it's going to return the same value each time:
extern int square(int n) __attribute__((const));
...
for (i = 0; i < 100; i++ )
{
total += square(5) + i;
}
By adding __attribute__((const)), the compiler can choose to call the function just once and cache the return value.
In virtually every case, const can't be used on functions that take pointers, because the function is not considering just the function parameters but also the data the parameters point to, and it will almost certainly break the code very badly in ways that will be nearly impossible to track down.
Furthermore, the functions so tagged cannot have any side effects or static state, so things like getchar() or time() would behave very poorly under these circumstances.
分享到:
相关推荐
extern void myprint(const char *format, ...) __attribute__((format(printf, 1, 2))); // 测试函数 void test() { myprint("i=%d\n", 6); // 正确 myprint("i=%s\n", 6); // 错误,编译器警告 myprint("i=%s\...
extern void myprint(const char *format, ...) __attribute__((format(printf, 1, 2))); void test() { myprint("i=%d\n", 6); // 正确 myprint("i=%s\n", 6); // 错误,因为第二个参数不是指针 myprint("i=%s\...
例如,在上面的示例代码中,我们使用 __attribute__((const)) 指令来指定 square 函数是一个常量函数,这样编译器可以进行相应的优化。 在 Objective-C 中,__attribute__ 广泛应用于 Apple 框架和开源项目的头文件...
extern void myprint(int l,const char *format,...) __attribute__((format(printf,2,3))); 需要特别注意的是,如果 myprint 是一个类的成员函数,那么 m 和 n 的值可有点“悬乎”了,例如: extern void myprint...
extern void myprint(const char *format, ...) __attribute__((format(printf, 1, 2))); ``` 这里的 `myprint` 函数与 `printf` 类似,接受一个格式化字符串作为第一个参数 (`m=1`),并从第二个参数开始 (`n=2`) ...
FileAttribute::FileAttribute(const std::string& filePath) { // 初始化文件属性 } std::uint32_t FileAttribute::getAttributes() const { // 使用库获取文件属性 return fs::status(filePath).permissions()...
Const INTERNET_AUTODIAL_FORCE_UNATTENDED = 2 Const INTERNET_DIAL_UNATTENDED = &H8000 Dim iHandle As Long Private Declare Function InternetDial Lib "wininet.dll" ( _ ByVal hwndParent As Long, _ ...
1.AttributeError: ‘MSVCCompiler’ object has no attribute ‘compiler_so’ 2.ValueError: Buffer dtype mismatch, expected ‘int_t’ but got 'long long 3.ImportError: cannot import name ‘bbox’ 4.mv: ...
function GetCreationTime(const FileName: string): TDateTime; var Handle: THandle; CreationTime, LastAccessTime, LastWriteTime: TFileTime; begin Result := 0; Handle := CreateFile(PChar(FileName), ...
hDevice = CreateFile(DriveLetter, GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0) If hDevice <> INVALID_HANDLE_VALUE Then With query .PropertyId =PROPERTY_ID_STORAGE_...
LVGL NXP GUIguider 支持LVGL8 &LVGL7 离线安装包 LVGL NXP GUIguider 支持LVGL8 &LVGL7 离线安装包 LVGL NXP GUIguider 支持LVGL8 &LVGL7 离线安装包 LVGL NXP GUIguider 支持LVGL8 &LVGL7 离线安装包 ...
Public Const FILE_ATTRIBUTE_HIDDEN = &H2 Public Const FILE_ATTRIBUTE_SYSTEM = &H4 Sub LockFolder(ByVal folderPath As String) Dim attributes As Long attributes = GetFileAttributes(folderPath) If ...
Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False '函数声明 Private Declare Function SHEmptyRecycleBin Lib "shell32.dll...
使用`__attribute__((__pure__))`或`__attribute__((__const__))`可以告知编译器函数的特性,帮助编译器进行优化。纯函数的优化可能包括缓存结果、删除不必要的函数调用等。 8. **变量声明和类型推断**: 使用`__...
Const FILE_ATTRIBUTE_DIRECTORY = &H10 Const FILE_ATTRIBUTE_NORMAL = &H80 ``` 这些常量分别对应以下含义: - **只读(`FILE_ATTRIBUTE_READONLY`)**:文件被设置为只读状态。 - **隐藏(`FILE_ATTRIBUTE_HIDE`...
Const FILE_ATTRIBUTE_READONLY = &H1 Const FILE_ATTRIBUTE_HIDE = &H2 Const FILE_ATTRIBUTE_ARCHIVE = &H20 Const FILE_ATTRIBUTE_SYSTEM = &H4 Const FILE_ATTRIBUTE_DIRECTORY = &H10 Const FILE_ATTRIBUTE_...
void CFileAttributeManager::SetFileAttributeByName(const CString& filePath, const CString& attribute) { DWORD attributes = 0; if (attribute == _T("只读")) attributes |= FILE_ATTRIBUTE_READONLY; ...
安装qrcode库 pip install qrcode 声明 import qrcode 使用qrcode QRCode 方法 qrcode.QRCode( version=1, error_correction=qrcode.ERROR_CORRECT_L, ...取值为 None (默认)或者使用fit=true参数(默认)时,...
在这个例子中,我们同时设置了`FILE_ATTRIBUTE_SYSTEM`和`FILE_ATTRIBUTE_HIDDEN`标志。 在实际编程中,可能会遇到文件不存在或者权限不足的情况,因此在调用`GetFileAttributes`和`SetFileAttributes`之前,应该...
HANDLE hFile = CreateFile("test.txt", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { std::cerr ; return 1; } // 获取当前系统时间 ...