[英]why does c++ std::min can't use a static field as its parameter when compile on O0?
同樣的代碼,用O0編譯,會報錯:
//============================================================================
// Name : test.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <stdint.h>
using namespace std;
class foo{
static const int64_t MAX_THREAD_NUM = 10 * 1000;
public:
void test();
};
void foo::test(){
int64_t a = 100;
// int64_t tmp = MAX_THREAD_NUM;
// int64_t min = std::min(tmp, a);
int64_t min = std::min(MAX_THREAD_NUM, a);
cout << min << endl; // prints !!!Hello World!!!
}
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/test.d" -MT"src/test.o" -o "src/test.o" "../src/test.cpp"
g++ -o "test" ./src/test.o
./src/test.o: In function `foo::test()':
/home/foo/eclipse-workspace/test/Debug/../src/test.cpp:27: undefined reference to `foo::MAX_THREAD_NUM'
collect2: error: ld returned 1 exit status
/home/foo/eclipse-workspace/test/Debug/../src/test.cpp:27: undefined reference to `foo::MAX_THREAD_NUM'
但是有O2標志,它可以編譯成功。
g++ -O2 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/test.d" -MT"src/test.o" -o "src/test.o" "../src/test.cpp"
g++ -o "test" ./src/test.o
g++ 版本:g++ (Ubuntu 4.8.5-4ubuntu8) 4.8.5 版權所有 (C) 2015 Free Software Foundation, Inc. 這是免費軟件; 請參閱復制條件的來源。 沒有保修; 甚至不是為了特定目的的適銷性或適合性
當您通過不提供 ODR 使用的符號的定義( std::min
通過引用獲取其參數)來破壞一個定義規則 (ODR) 時,程序格式不正確,不需要診斷 (NDR)。
優化器會刪除那些未使用的代碼,讓您認為它是正確的。
正如@dfri 所指出的,編譯器內聯MAX_THREAD_NUM
。 如果我們把它改成inline static const int64_t MAX_THREAD_NUM = 10 * 1000;
,它將在O0
上編譯良好。 否則,在類之外聲明但定義變量,或將其標記為constexpr
而不是const
。 如果在需要非常量積分的地方使用靜態常量積分成員,則需要定義它。
class foo {
static const int64_t MAX_THREAD_NUM;
...
};
const int64_t foo::MAX_THREAD_NUM = 10 * 1000;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.