Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 128 129 [130] 131 132 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 907241 times)

eerr

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1935 on: March 09, 2012, 03:00:36 pm »

....

See this is the problem with being self taught in things. I didn't know function pointers existed in c++ until I read this page, and there are innumerable things I could've made much simpler by using them. Looking at my usual documentation, function pointers are hidden away as a footnote, so I never noticed it. 

/rage

Function pointers also got some inherent problems too-

If they aren't Cdecl you have to specify the type- and you can't mix any of the types.
If it takes Cdecl and you give it stdcall it breaks.

and if you already have something compiled to use stdcall and have something that uses cdecl it can be hard to figure why it's just not working.
You have to wrap them into other function(s) with only a single calling convention, or change them both to the same calling convention.

Oh, and you probably loose all cross compatibility.
Logged

kaenneth

  • Bay Watcher
  • Catching fish
    • View Profile
    • Terrible Web Site
Re: if self.isCoder(): post() #Programming Thread
« Reply #1936 on: March 09, 2012, 03:09:01 pm »

I can no longer respect a language that doesn't have functions/methods as first-class objects.



Logged
Quote from: Karnewarrior
Jeeze. Any time I want to be sigged I may as well just post in this thread.
Quote from: Darvi
That is an application of trigonometry that never occurred to me.
Quote from: PTTG??
I'm getting cake.
Don't tell anyone that you can see their shadows. If they hear you telling anyone, if you let them know that you know of them, they will get you.

eerr

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1937 on: March 09, 2012, 03:19:53 pm »

I can no longer respect a language that doesn't have functions/methods as first-class objects.

What about a language that doesn't have variables?
Logged

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1938 on: March 09, 2012, 03:22:22 pm »

Function pointers in c++ are really really weird. I heard the reason for this was that the c++ standard was really slow at coming with any definitive specification for how to handle them, so different compiler implemented them on their own.
In my honest opinion the reason why the use of function pointers is restricted is Because:
A: All the functionality can be implemented using OOP.
B: It's really easy to introduce bugs as the compiler won't catch any programmer mistake.

While the implementation is standard when it comes to global functions, it's very murky when it comes to class methods. In one situation I was trying to speed up virtual methods by simply using method pointers and as such dropping the vtable, but I found that even if I could technically cast from one class method to another, the returned address was for whatever reason offset by 256 bytes or so.
I never could find any logical explanation for why this was.
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #1939 on: March 09, 2012, 04:31:40 pm »

std::function is pretty cool at hiding that stuff and is first-class, so it opens some of the flexibility of that world up to C++. I think for a member function the overhead is an extra call due to how it generically binds things (union hacks ftw >.<).

Gaze at the madness that is a variadic template adder (got bored) ye mighty and despair:
Code: [Select]
template<typename... ARGS> struct add { };

template<typename A, typename B> struct add<A,B>
{
typedef const A& param1;
typedef const B& param2;
typedef decltype(A()+B()) rettype;
rettype operator()(param1 a, param2 b)  const
{
return a + b;
}
};

template<typename A, typename B, typename C> struct add<A,B,C>
{
typedef const A& param1;
typedef const B& param2;
typedef const C& param3;
typedef add<A,B> abadder;
typedef typename abadder::rettype retabtype;

typedef add<retabtype,C> abcadder;
typedef typename abcadder::rettype retabctype;
typedef retabctype rettype;

rettype operator()(param1 a, param2 b, param3 c)  const
{
return abcadder()(abadder()(a,b), c);
}
};

template<typename A, typename B, typename C, typename... D> struct add<A,B,C,D...>
{
typedef const A& param1;
typedef const B& param2;
typedef const C& param3;
typedef add<A,B> abadder;
typedef typename abadder::rettype retabtype;

typedef add<retabtype,C> abcadder;
typedef typename abcadder::rettype retabctype;
typedef retabctype rettype;

rettype operator()(param1 a, param2 b, param3 c, const D& ... d)  const
{
return add<retabctype, D...>()(abcadder()(abadder()(a,b), c), d...);
}
};

template<typename... A> typename add<A...>::rettype addition(const A&... a)
{
return add<A...>()(a...);
}

Should preserve standard operator precedence for a + b + c + d + e, which is (((a + b) + c) + d) + e. The easier way of doing this, simply to recursively call a + sum(b...) would do a + (b + (c + (d + e))). Well, that kept me distracted for half an hour wading through template errors -_-

You could probably implement such a thing without C++11 features (replacing decltype would be tricky though) via loads more specialisations created by a script.

Now I just need to find a use for this. Aside from accidentally discovering that gcc has some features with variadics not yet implemented in 4.6.0.

EDIT: And now I've genericnessificated it so it'll work for all operations. Yep, really bored.
« Last Edit: March 09, 2012, 05:27:17 pm by MorleyDev »
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1940 on: March 09, 2012, 06:04:18 pm »

If I never see the keyword typename again in my life I will not be unhappy. C++ is too wordy with its keywords (especially with C++11), and they mean different things in different contexts.

With that said, I would still choose C++ over any other language, except maybe Perl and Python.

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1941 on: March 09, 2012, 07:40:59 pm »

If I never see the keyword typename again in my life I will not be unhappy. C++ is too wordy with its keywords (especially with C++11), and they mean different things in different contexts.

With that said, I would still choose C++ over any other language, except maybe Perl and Python.

"typename" is often synonymous with "class" when declaring templates. Which can be confusing.

The difference is that typename refers to a type, while class refers to a class.
It can be very useful in some situations.
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #1942 on: March 09, 2012, 07:55:02 pm »

I think his issue is how you have to declare typename X::Y to use Y when X is a template parameter.
Logged

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1943 on: March 09, 2012, 08:28:28 pm »

I think it does makes sense. The compiler does not know much about the code when using templates, so specifying that it's the type we need makes sense, it could be a static class or function for all it knows.
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #1944 on: March 09, 2012, 11:53:17 pm »

So I'm testing some approximations of the square root for calculating the magnitude of a vector (the earliest adder came as a result of making generic swap-in swap-out functors to use for this starting at the bottom and going up from there). The "fast" versions of magnitude and inverse magnitude rely on 32-bit IEE notation but the results are interesting. Premature optimisation is evil 'n' all that, I'm just curious.

I used a seeded random number generator to try and eliminate the optimisations from the compiler.

For std::sqrt, 1.0 / std::sqrt, fast sqrt (which uses the bitwise approximation for IEE) and inverse fast sqrt (which uses Quake's algorithm) I basically learnt that std::sqrt is pretty damn efficient. Division? Not so much.
« Last Edit: March 10, 2012, 01:37:12 am by MorleyDev »
Logged

Orangebottle

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1945 on: March 10, 2012, 12:37:52 am »

....

See this is the problem with being self taught in things. I didn't know function pointers existed in c++ until I read this page, and there are innumerable things I could've made much simpler by using them. Looking at my usual documentation, function pointers are hidden away as a footnote, so I never noticed it. 

/rage

That's why I'm super happy with the book I'm using to teach myself, it goes really slow but covers everything very in depth.
What book is it?
Logged
My Sig
Quote from: The Binder of Shame: RPGnet Rants
"We're in his toilet. We're in Cthulhu's toilet."

""Hey! No breaking character while breaking character"

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1946 on: March 10, 2012, 12:59:48 am »

....

See this is the problem with being self taught in things. I didn't know function pointers existed in c++ until I read this page, and there are innumerable things I could've made much simpler by using them. Looking at my usual documentation, function pointers are hidden away as a footnote, so I never noticed it. 

/rage

That's why I'm super happy with the book I'm using to teach myself, it goes really slow but covers everything very in depth.
What book is it?
I also would like to know!
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1947 on: March 10, 2012, 01:28:13 pm »

I think it does makes sense. The compiler does not know much about the code when using templates, so specifying that it's the type we need makes sense, it could be a static class or function for all it knows.

I'm not saying it doesn't make sense. I'm saying that, in projects that make heavy use of templates, that keyword appears almost as much as actual code does.

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1948 on: March 10, 2012, 04:45:51 pm »

Can't really argue about that.
I have never tried to create a project making so heavy use of templates, so I never really seen it as an noyance.

I have the tendency template the shit out of everything I can get away with. I love the fact that you write one base class, and the compiler potentially spits out 400 different versions of that class, to suit your every need.
... Yeah I will probably never do good at those 64k challenges.
« Last Edit: March 10, 2012, 04:49:16 pm by malloc »
Logged

Mini

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1949 on: March 12, 2012, 09:05:41 am »

How not to explain namespaces: "It's like if you have a car, with a toaster in it, and you wouldn't look for your toaster in your car so that's what the whole :: stuff is for, telling it to look in your car, and then #include is making you have a car". Slightly paraphrased, but I think it gets the point across. (This is me explaining to a friend, not someone explaining to me.)
Logged
Pages: 1 ... 128 129 [130] 131 132 ... 796