logo
down
shadow

Compile-time check of functor


Compile-time check of functor

By : HARSH GUPTA
Date : November 29 2020, 09:01 AM
wish of those help You can use this callable_traits to get return type and argument type of the functor, and use std::is_same to do assertion in static_assert
code :
// callable_traits

namespace detail {
    template <class ReturnType, class... Args>
    struct callable_traits_base
    {
        using return_type = ReturnType;
        using argument_type = std::tuple<Args...>;

        template<std::size_t I>
        using arg = typename std::tuple_element<I, argument_type>::type;
    };
}

template <class T>
struct callable_traits : callable_traits<decltype(&T::operator())>
{};

// lambda / functor
template <class ClassType, class ReturnType, class... Args>
struct callable_traits<ReturnType(ClassType::*)(Args...) const>
: detail::callable_traits_base<ReturnType, Args...>
{};
struct Hash {
  uint32_t operator ()(const char *buffer, size_t n) const {
    return 65;
  }
};

static_assert(std::is_same<callable_traits<Hash>::return_type, uint32_t>::value, "");
static_assert(std::is_same<callable_traits<Hash>::argument_type, std::tuple<const char *, size_t>>::value, "");


Share : facebook icon twitter icon
run-time vs. compile-time iPhone version check

run-time vs. compile-time iPhone version check


By : Andre
Date : March 29 2020, 07:55 AM
around this issue __IPHONE_OS_VERSION_MIN_REQUIRED is indeed just a build setting, which you can use in the preprocessor to modify code before compilation. No, Apple doesn't compile your code for each iPhone. They can't, since you don't give them the code. You can use compile time checking to determine, for example, if you need to compile code to emulate a new feature in older OS versions. However, you use runtime version checking to determine whether you should use the built in or emulated version of that feature.
For example, iAds were added in 4.0. You currently support 4.0 and later, but plan on adding support for 3.2 in the future. You make code to display other ads in older versions, using runtime checking to determine whether you should use iAds or other, but you don't want it to be in any releases until the rest of the application is ready for 3.2, since it will make your application larger. You use compile time checking so that the preprocessor leaves that code out of your releases. By using the minimum version macro, you can easily add the code to your release by changing the minimum version build setting.
How to replace run-time instanceof check with compile-time generics validation

How to replace run-time instanceof check with compile-time generics validation


By : user280813
Date : March 29 2020, 07:55 AM
will be helpful for those in need Got a little puzzle for a true Java Generics specialist... ;) , This is uglier than I thought. My take:
code :
interface Processor<F extends Foo<F>> {
    void process(F foo);
}

interface Foo<F extends Foo<F>> {
    Processor<F> getProcessor();
}

interface SomeFoo extends Foo<SomeFoo> {
    @Override
    SomeProcessor getProcessor();
}

interface SomeProcessor extends Processor<SomeFoo> {
    @Override
    void process(SomeFoo foo);
}
<F extends Foo<F>> void process(F foo) {
    foo.getProcessor().process(foo);
}
void process(Foo<?> foo) {
    foo.getProcessor().process(foo);
}
    class Bar implements Foo<SomeFoo> { ... }
abstract class Foo<F extends Foo<F>> {
    abstract Processor<F> getProcessor();

    abstract F getThis();
}

class SomeFoo extends Foo<SomeFoo> {
    @Override
    SomeFoo getThis() {
        return this;
    }

    @Override
    Processor<SomeFoo> getProcessor() {
        return new SomeProcessor();
    }
}
<F extends Foo<F>> void process(Foo<F> foo) {
    foo.getProcessor().process(foo.getThis());
}
Foo<?> foo = ...;
process(foo);
abstract class Foo<F extends Foo<F>> {
    abstract Processor<F> getProcessor();

    abstract F getThis();

    void processWith(Processor<F> p) {
        p.process(getThis());
    }
}
Check some compile-time definitions at compile time with older C++ implementations

Check some compile-time definitions at compile time with older C++ implementations


By : user2483376
Date : March 29 2020, 07:55 AM
seems to work fine When working on a large legacy code base, I today suspected a duplicate definition, but the dependency was not obvious to me human since it depended on a lots of compile-time calculations. , This works:
code :
#define STATIC_ASSERT(x) typedef char foo[(x) ? 1 : -1];
#define BOOST_JOIN( X, Y ) BOOST_DO_JOIN( X, Y )
#define BOOST_DO_JOIN( X, Y ) BOOST_DO_JOIN2(X,Y)
#define BOOST_DO_JOIN2( X, Y ) X##Y
#define STATIC_ASSERT(x) \
    typedef char BOOST_JOIN(violation_on_line_,__LINE__) [(x) ? 1 : -1];
Functor compile time error

Functor compile time error


By : user3275281
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Based on the clarification in the comment ("I am trying to fill the contents of my container such with incrementing values") what you really want is std::iota. I haven't quite gotten straight what you're trying to do with your matrix, and how you want the values to increment (row-wise or column-wise), so I'll give a demo with a normal vector, and let you figure out how to apply it in your actual situation:
code :
std::vector<int> v(10);

std::iota(v.begin(), v.end(), 22.2);

for (auto i : v)
    std::cout << i << " ";
Compile time check AND runtime check 'at the same time'

Compile time check AND runtime check 'at the same time'


By : mariuszeks607
Date : October 03 2020, 02:00 PM
With these it helps A typical way to do in C with gcc compiler, that will also work in C++: you use __builtin_constant_p builtin to check if an expression is a constantly evaluated and then check the expression and then call a function declared with a __attribute__((__warning__)) or with __attribute__((__error__)). Like so:
code :
#include <cassert>
#include <type_traits>

#define CONCAT(a, b) a ## b
#define XCONCAT(a, b) CONCAT(a, b)
#define maybe_static_maybe_not_assert(expr) do { \
    if (__builtin_constant_p(expr)) { \
            if (!(expr)) { \
                extern __attribute__((__warning__( \
                "static_assert: expression: " #expr " will fail on runtime!" \
                ))) void XCONCAT(maybe_static_maybe_not_assert_warn, __LINE__)(); \
              XCONCAT(maybe_static_maybe_not_assert_warn, __LINE__)(); \
         } \
    } \
    assert(expr); \
} while(0)

struct Dimensions {

    Dimensions& operator=(int i) {
        maybe_static_maybe_not_assert(i != 0);
        return *this;
    }

};

int getDim();

int main() {
    Dimensions dims;
    dims = getDim();
    dims = 0;
    dims = 1;
    return 0;
}
In member function 'Dimensions& Dimensions::operator=(int)',
    inlined from 'int main()' at <source>:32:12:
<source>:12:70: warning: call to 'maybe_static_maybe_not_assert_warn21' declared with attribute warning: static_assert: expression: i != 0 will fail on runtime! [-Wattribute-warning]
   12 |                 XCONCAT(maybe_static_maybe_not_assert_warn, __LINE__)(); \
      |                                                                      ^
<source>:21:9: note: in expansion of macro 'maybe_static_maybe_not_assert'
   21 |         maybe_static_maybe_not_assert(i != 0);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compiler returned: 0
shadow
Privacy Policy - Terms - Contact Us © animezone.co