« first day (1102 days earlier)      last day (1753 days later) » 

8:39 AM
template <class T>
void func(args) {
    if(std::same<T, MyType>::value) {
         /* extra code for MyType */
    }

    /* common code */
}
I want the code inside the if branch to be ignored if the condition fails.
Something like constexpr if but in c++11
I can specalize func but that will lead to the common code being duplicated.
 
9:16 AM
Hello guys! Why do we have std::atomic_flag_test_and_set[_explicit]() and std::atomic_flag_clear[_explicit]() when std::atomic_flag already has
test_and_set() and clear() members? Seems like a false abstraction to me.
@Yashas What about putting the common code into another function? Alternatively you could put the extra code into another function template that is empty by default and specialized for MyType. I don't think you can avoid one additional function (template).
 
9:32 AM
I am trying to avoid another function
 
9:43 AM
put common code in a function that gets called by both specializations
 
 
3 hours later…
12:29 PM
@Yashas if constexpr?
 
12:39 PM
yes, something equivalent for c++11
what are the ways to prevent a compiler from evaluating a code block? templates aren't evaluated until they are instantiated. What else can be abused?
 
@Yashas if the compiler can detect the if is constant then it should optimize it out
 
@Mgetz I don't want the compiler to process the code in the branch
 
if constexpr is mostly a way to tell the compiler that you REALLY want it optimzied out
@Yashas then you'd have to use macros
even if constexpr won't get you that
 
I think constexpr if doesn't evaluate the branch.
 
when in doubt... godbolt it
 
12:43 PM
Constexpr If

The statement that begins with if constexpr is known as the constexpr if statement.

In a constexpr if statement, the value of condition must be a contextually converted constant expression of type bool. If the value is true, then statement-false is discarded (if present), otherwise, statement-true is discarded.
it says discarded which probably means not evaluated
well, at least gcc doesn't
I have used it before
17
A: Difference between "if constexpr()" Vs "if()"

BrianThe ordinary if statement: Has its condition evaluated every time control reaches it, if ever Determines which of the two substatements to execute, skipping the other Requires both substatements to be well-formed regardless of which one is actually selected at runtime The if constexpr stateme...

answer says "Does not require the discarded substatement to be well-formed"
 
appears to evaluate
or at least parse it
"Outside a template, a discarded statement is fully checked. if constexpr is not a substitute for the #if preprocessing directive". Source: en.cppreference.com/w/cpp/language/ifTarquiscani Aug 1 '18 at 16:15
 
I am surprised. I clearly remember having a template instantiation which would fail inside a constexpr branch.
 
The other answer to the same question yields an example that demonstrates the difference. stackoverflow.com/a/43434771/1007605
 
It's not the same as #if of course because the compiler still has to parse it.
 
12:50 PM
@Yashas it doesn't have to be well formed, it does have to be defined it looks like
 
Well it's the same rules as in function templates in general I think. Just like you can't simply define a function template where you use some variable that does not exist.
You can not expect it to work even if you never instantiate it - same with your if constexpr (false).
 
My use-case in the current c++11 code I am dealing with requires failing instantiations in a constexpr branch.
I have syntatically valid code inside it.
 
Therefore, I think there is no other way to have the same functionality as if constexpr without another function (template) because they basically do the same thing.
 
@purefanatic tag dispatch I believe can do it somewhat... but not completely
 
template <class T>
void relu(const Stream& stream, span<T> output, view<T> input, T slope) {
    if(std::is_same<T, __half>::value) {
        /* try vector 16 kernel */
    }

     if(std::is_same<T, __half>::value || std:is_same<T, float>::value) {
        /* try vector 8 kernel */
    }

    if(is_fully_aligned<T>(output, 4) && is_fully_aligned<T>(input, 4)) {
        launch_vectorized_relu<T, 4>(stream, output, input, slope);
    } else if (is_fully_aligned<T>(output, 2) && is_fully_aligned<T>(input, 2)) {
that's the exact code
 
12:58 PM
@Yashas for this... use tag dispatch
it really is the right choice
 
instantating launch_vectorized_relu inside the first two if conditions will fail
for types which are not in the condition
@Mgetz it requires more functions which is what I am trying to avoid
with c++17, this would be much cleaner code in a single function
 
@Yashas well your options are C++17 or doing it the pre c++17 way
 
@Yashas Yeah but I think you might be out of luck here... Better upgrade to C++17 ;-)
 
there is a hack which boost hana::eval_if uses but it works in c++14 only
requires generic lambdas
I was hoping to find something similar for c++11
I don't mind having constexpr_if<true>([] { });
 
1:14 PM
@Yashas Hm in the example you gave, could you maybe just use a constexpr variable like constexpr int align = is_fully_aligned<T>(output, 4) && is_fully_aligned<T>(input, 4) ? 4 : is_fully_aligned<T>(output, 2) && is_fully_aligned<T>(input, 2) ? 2 : 1;
And then call launch_vectorized_relu<T, align>()
Oh I overread that you need it for the first two if conditions...
 
and is_fully_aligned is not a constexpr function
 
1:33 PM
I see...
 
2:24 PM
@Justin Thanks for the reply! I hoped there was a way I could do it only from the base class to avoid modifying the whole code base. Maybe typeid is what I'm after?!
 
@AlexandreVaillancourt Doesn't work from the constructor: godbolt.org/z/eKThSK . During construction of the Base object, the object is literally a Base at that time
 
@Justin Ah crap, yeah, this totally makes sense.
 
"I want to know what objects are still alive when I shut down my app" maybe have a global (possibly thread_local) set that you insert the this pointer into? std::unordered_set<Base*> alive, in Base constructor, alive.insert(this), in destructor, alive.erase(this)
 
2:41 PM
@Justin Yeah, that's the gist of it. But now you just made me realize that I don't need the type in the ctor, only when I quit the app; and I guess typeid will work at that moment!
 
It should
 
Thanks a lot!!
 
 
2 hours later…
4:42 PM
Yeah, it worked, thanks again!
 
Nice
np
 

« first day (1102 days earlier)      last day (1753 days later) »