Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jan 31 15:06

    ldionne on gh-pages

    Update benchmarks to 490dbf6 fo… (compare)

  • Jan 24 07:47

    ldionne on gh-pages

    Update benchmarks to 490dbf6 fo… (compare)

  • Jan 23 02:04
    pthom commented #432
  • Jan 18 12:46

    ldionne on gh-pages

    Update benchmarks to 490dbf6 fo… (compare)

  • Jan 16 22:27
    ricejasonf commented #434
  • Jan 05 06:22
    ricejasonf commented #330
  • Jan 03 11:40

    ldionne on gh-pages

    Update benchmarks to 490dbf6 fo… (compare)

  • Jan 02 00:33
    ricejasonf closed #434
  • Jan 02 00:33
    ricejasonf commented #434
  • Jan 02 00:04
    ricejasonf opened #434
  • Dec 27 2018 13:11

    ldionne on gh-pages

    Update benchmarks to 490dbf6 fo… (compare)

  • Dec 22 2018 11:56
    pthom commented #432
  • Dec 22 2018 11:55
    pthom commented #432
  • Dec 21 2018 15:48
    pthom synchronize #432
  • Dec 21 2018 09:19
    sdebionne opened #433
  • Dec 21 2018 00:08
    ricejasonf commented #432
  • Dec 21 2018 00:03
    ricejasonf commented #432
  • Dec 20 2018 23:36
    pthom commented #432
  • Dec 20 2018 23:36
    pthom commented #432
  • Dec 20 2018 23:13
    ricejasonf commented #432
Jason Rice
@ricejasonf
well I totally forgot how to do syntax highlighting on gitter
it only works for non-static variables though
Jason Rice
@ricejasonf
err here it is with all of the stuff removed https://godbolt.org/z/G3LaMY
Shreyans
@shreyans800755
That's exactly what I want. But I wanted non-macro solution if possible. I don't think its possible to have with current compilers. I guess I'll have to wait till Herb Sutter's meta classes proposal's acceptance & implementation :)
Thanks for the hana solution. Its more compact & clean than what I was trying to write with macros
Jason Rice
@ricejasonf
:+1:
deepgrace
@deepgrace
:)
Shreyans
@shreyans800755

I'm trying to calculate gcd of two int at compile time.



template <int a, int b>
struct Gcd
{
 enum { value = (a > b) ? (Gcd<a-b, b>::value) : ((a < b) ? (Gcd<a,b-a>::value) : a)};
};

template <int a>
struct Gcd<a, 0>
{
 enum { value = a};
};

template < int b>
struct Gcd<0, b>
{
  enum { value = b};
};

constexpr int GCD(int a, int b)
{
    if(a * b == 0)
        return a + b;
    if(a > b)
        return GCD(a - b, b);
    if(a < b)
        return GCD(a, b - a);
        return a;
}

int main()
{

    return (Gcd<24, 12>::value); // error
    return GCD(24, 12); // works fine.
}

This code goes into infinite recursion with template instantiations. To debug it more,
I tried to set the -ftemplate-depth=2, and it failed at Gcd<12, 12> (required from Gcd<24, 12>) as expected. But if I increase the depth to 3, it fails at Gcd<36, -12> (required from Gcd<24, -12> required from Gcd<24, 12>). I'm not sure why is it giving such unexpected behavior.
However, the constexpr GCD function works as expected.
Has anyone faced similar issues with enum TMP ?

I know this is not the best way to find gcd, but I'm just trying out something
Shreyans
@shreyans800755
The template only works if either at least one param is 0, or both params are same. So, Gcd<12, 12> returns 12 as expected.
deepgrace
@deepgrace
#include <utility>

template <int x, int y>
struct gcd
{
    using type = typename std::conditional_t<x * y == 0, std::integral_constant<int, x + y>, gcd<y, x % y>>::type;
};

template <int y>
struct gcd<0, y>
{
    using type = std::integral_constant<int, y>;
};

template <int x>
struct gcd<x, 0>
{
    using type = std::integral_constant<int, x>;
};

template <int x, int y>
using gcd_t = typename gcd<x, y>::type;

template <int x, int y>
inline constexpr auto gcd_v = gcd_t<x, y>::value;

int main(int argc, char* argv[])
{

    static_assert(gcd_v<4, 4> == 4);
    static_assert(gcd_v<4, 18> == 2);
    static_assert(gcd_v<7, 13> == 1);

    return 0;
}
deepgrace
@deepgrace
you need lazy evaluation
Shreyans
@shreyans800755
Thanks for looking into it.
I was able to do this with integral constant, of course. But wasn't sure why template instantiation was giving bizarre behavior in my code. I tried to look through the standard, but couldn't find anything relevant for ternary evaluation in enums at compile time. Thanks anyways
deepgrace
@deepgrace
enum { value = std::conditional_t<(a > b), Gcd<a - b, b>, Gcd<a, b - a>>::value };
just some adjustment
std::conditional_t is compile time ternary evaluation
Quentin Chateau
@qchateau
Hi, I opened a Pull Request on GitHub 2 weeks ago. I don't know how often you go there to check for new PR, I just wanted to make sure it did not go unnoticed :)
Jason Rice
@ricejasonf
maybe ping Louis, he probably just got busy and forgot idk
deepgrace
@deepgrace
The Art of Template MetaProgramming in Modern C++
https://github.com/deepgrace/monster
Jason Rice
@ricejasonf
hypercube_indices.. neat
what would you use that for?
neat project anyways
deepgrace
@deepgrace
It is equivalent to N identical nested loops. The algorithm is somewhat simple, and used to enumerate multidimensional
indices in hypercubes. Just for fun, maybe.
Samuel Debionne
@sdebionne
Hi all,
I have a metaprogram on godbolt that transforms the signature of Struct member functions to a tuple. I try to use Hana value-based metaprogramming and my question is regarding the unwrapping of Hana.Type. At some point, I need to unwrap the result of callable_traits::args and I am either getting error: static assertion failed: declval() must not be used! or error: lambda-expression in unevaluated context depending on where the lambda argument to hana::transform is declared. The second error should be fixed in C++20 but, since then, is there any way around this?
Samuel Debionne
@sdebionne
Well, I think that hana::traits::declvalis what I need, sorry for the noise.
Erik Zenker
@erikzenker
Hi all
Pranam Lashkari
@lpranam
I am trying to run the following example with the given real-world example in the documentation
boost::any x;
x = 23;
auto var = switch_(x)(
        case_<int>([x](auto) -> int { return boost::any_cast<int>(x); }),
        case_<std::string>([x](auto) -> std::string { return std::string("hello"); }),
        default_([]() -> long long { return 3ll; })
        );
it gives me the following errors:
C2446    ':': no conversion from '__int64' to 'std::string'
C3313    'var': variable cannot have the type 'void'
Is there any way to achieve this goal?
I have just copied the source from the documentation and tried to run it without any changes.
Jason Rice
@ricejasonf
maybe var needs to be explicitly boost::any
err idk sorry Im on my phone
Pranam Lashkari
@lpranam
That would kill the purpose. I am trying to get the stored data inside boost::any. I.e the way i have done in case_<int>
Jason Rice
@ricejasonf
what is the type of var?
ah.. yeah I think your lambdas need to return all of the same type
looking at the example code
Pranam Lashkari
@lpranam
Driver code used in example:
boost::any a = 'x';

auto r = switch_(a)(

case_<int>([](auto) -> int { return 1; }),

case_<char>([](auto) -> long { return 2l; }),

default_([]() -> long long { return 3ll; })

);

// r is inferred to be a long long

static_assert(std::is_same<decltype(r), long long>{}, "");

assert(r == 2ll)
Lambdas are returning different types
Jason Rice
@ricejasonf
right but they can be cast to the same type
the result of switch_ has to have a type
Pranam Lashkari
@lpranam
So is there any way to use these utility with classes which are not convertible into each other!?
Jason Rice
@ricejasonf
using the visitor pattern yes
or reduce it to a single type
I've not used boost::any much, but this is a common use case for variant
Pranam Lashkari
@lpranam

Visitor pattern would fail in my case.

What i am trying to do is i read a table from a file. Data stored in the table can be of type int, string, char, double, float, etc.
Each column is of what type is also given in the file. So there is no way to know what type is in table until execution. If i store data in boost::any then to retrieve actual data from it we need to use any_cast which requires a template parameter which again is not available at compile time.

This is part of my gsoc project. I am developing a new astronomy library intended to be a part of boost

Jason Rice
@ricejasonf
I guess it depends on what you expect var to be or what you need to do with it
I highly recommend http://cpplang.slack.com/. There is a #boost-users channel. Again I would use variant if the types are a closed set.
Jason Rice
@ricejasonf
That switch_ function is really just a fancy visitor. Each lambda visits the contained value if it matches the type in its case_
Abhishek Sharma
@abhishhh1
Hello everyone, I am Abhishek Sharma, a pre-final year undergraduate from india. I am newbie to open source development but have good knowledge of C++ , Data structures and Algorithms. It would be really helpful if anyone could get me started.
deepgrace
@deepgrace
welcome