From: Nemo Yu Sent: Wednesday, January 27, 2016 9:22 PM Reply To: std-dis...@isocpp.org Subject: [std-discussion] Does the standard promise to remove unnecessary parameters? |
This is the 'as if' rule.
How could the call cite (which does not necessarily have access to the definition) know that a variable is unused?
As long as the observable results are the same, the compiler can do whatever.
In the standard there is no stack. Parameters are passed by magic.
This is entirely up to the ABI. If you want to influence
the portable ABI used by GCC and Clang
If T is not optimized, T does occupy space and waste performance.template<typename T> inline void foo(int, T) {} // T is never used.
From: Nemo Yu Sent: Wednesday, January 27, 2016 10:09 PM |
Reply To: std-dis...@isocpp.org |
Subject: Re: [std-discussion] Does the standard promise to remove unnecessary parameters? |
Thank you for replying.This is the 'as if' rule.I don't totally understand the words "as if" in English (forgive me not a native speaker), may you explain it somehow?
How could the call cite (which does not necessarily have access to the definition) know that a variable is unused?As long as the observable results are the same, the compiler can do whatever.I think the caller and called function can have an agreement: callers never push a tail argument marked as "unnecessary" and called functions never pop it, then the stack is balanced, as long as the ignored one is tail but not middle. A special case is inline functions, I think it can always ignore the unnecessary argument for compile period inline(I don't know if it is for link period inline).
However, as what Tony said:In the standard there is no stack. Parameters are passed by magic.It does make sense......
> If T is not optimized, T does occupy space and waste performance.
Yes. If that happens with your compiler, file a bug.
struct A{}; struct B{};
void foo(int, A) { /* It is too long to be an inline function */ }
void foo(int, B) { /* It is too long to be an inline function */ }
struct A{}; struct B{};
void foo_A(int) { /* It is too long to be an inline function */ }
void foo_B(int) { /* It is too long to be an inline function */ }
Is it a better way for performance?inline void foo(int i, A) { foo_A(i); }inline void foo(int i, B) { foo_B(i); }
The fact that it's a template is irrelevant.
The fact that it's inline is. If a function gets inlined, then there's no
call.
--
#include <vector>#include <iostream>int main(){std::vector<int> vec(10);for(auto i: vec)std::cout << i << std::endl;std::cout << "done" << std::endl;return 0;}
e.g.struct A{}; struct B{};void foo(int i, A);void foo(int i, B);when I call foo(0, A()), will the argument A be pushed into the stack or not? I mean, by the standard, will it be forced or suggested to ignore the unnecessary parameters/arguments?I give another example.void foo(int, A, int);void foo(int, B, int);How is it going? I don't think in this case the argument A can be ignored, or the sequence of arguments will be unknown for the foo function, if the caller do ignore A.
You may declare template in a header and define (and
explicitly instantiate) it in one translation unit.
> it when calls.
The standard does not use the term 'header' except for standard library
headers. Template definition does not need to be present in the
translation unit if you use explicit instantiation declaration (see
[temp.explicit]).
--
Regards,
Mikhail Maltsev
From: Nemo Yu Sent: Thursday, January 28, 2016 1:01 AM |
Reply To: std-dis...@isocpp.org |
Subject: Re: [std-discussion] Does the standard promise to remove unnecessary parameters? |