问题
Consider the following code (LWS):
#include <iostream>
#include <chrono>
inline void test(
const std::chrono::high_resolution_clock::time_point& first,
const std::chrono::high_resolution_clock::time_point& second)
{
std::cout << first.time_since_epoch().count() << std::endl;
std::cout << second.time_since_epoch().count() << std::endl;
}
int main(int argc, char* argv[])
{
test(std::chrono::high_resolution_clock::now(),
std::chrono::high_resolution_clock::now());
return 0;
}
You have to run it several times because sometimes, there is no visible difference. But when there is a visible difference between the time of evaluation of first
and second
, the result is the following under g++ :
1363376239363175
1363376239363174
and the following under intel and clang :
1363376267971435
1363376267971436
It means that under g++, the second
argument is evaluated first, and under intel and clang the first
argument is evaluated first.
Which one is true according to the C++11 standard?
回答1:
Which one is true according to the C++11 standard ?
Both are permissible. To quote the standard (§8.3.6):
The order of evaluation of function arguments is unspecified.
回答2:
I have a slightly simpler example to illustrate the same problem.
bash$ cat test.cpp
#include <iostream>
using namespace std;
int x = 0;
int foo()
{
cout << "foo" << endl;
return x++;
}
int bar()
{
cout << "bar" << endl;
return x++;
}
void test_it(int a, int b)
{
cout << "a = " << a << endl
<< "b = " << b << endl;
}
int main(int argc, const char *argv[])
{
test_it(foo(),bar());
return 0;
}
bash$ clang++ test.cpp && ./a.out
foo
bar
a = 0
b = 1
bash$ g++ test.cpp && ./a.out
bar
foo
a = 1
b = 0
来源:https://stackoverflow.com/questions/15440833/g-vs-intel-clang-argument-passing-order