Thread: How efficient is functional programming C++?

  1. #16
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Hey, thanks! I was thinking that, actually. Functors are pretty cool. I like this std::function thing though.

    Part of me is very surprised that std::function is part of C++11. I assumed it was old.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, the idea of fuctors is not all that new. Pre-C++11 had std::bind??? functions which produced functors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Okay, a quick question:
    Code:
    std::atomic_flag lock = ATOMIC_FLAG_INIT;
       
    template <class F> auto spinLockExecutor(F& f) -> decltype(f())
    {
      while (lock.test_and_set(std::memory_order_acquire)) {
           
      }
         
       auto && r = f();
         
      lock.clear(std::memory_order_release);
       
      return r;
    }
    What if f() has the following declaration:
    Code:
    void f(void)
    ?

    My compiler is yelling at me about returning a reference to void which makes sense. How do I overcome this? The universal reference collapse seems to turn void into a reference.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Separate the cases when the return type is void and when not. Not tested code, but:

    Code:
    template<typename T> struct Tag {}; // Helper type that's default-constructible
    
    template<typename F>
    void SpinLockExecutorHelper(F f, Tag<void>) -> void
    {
        // Implement me
    }
    
    template<typename F, typename R>
    void SpinLockExecutorHelper(F f, Tag<R>) -> R
    {
        // Implement me
    }
    
    template<typename F>
    void SpinLockExecutor(F f) -> decltype(f())
    {
        return SpinLockExecutorHelper(f, Tag<decltype(f())>());
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    So, I haven't run the code yet but what you gave me 100% compiles squeaky clean. And it makes a lot of sense too. That was a really quick response with a good approach to when templating code gets a little bit trickier.

    I really appreciate the help, you guys!

    Now my code is gonna be, like, super amazing XD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functional testing
    By g4j31a5 in forum Tech Board
    Replies: 15
    Last Post: 03-22-2012, 01:54 AM
  2. My functional syllogism program
    By jeremy duncan in forum C Programming
    Replies: 3
    Last Post: 12-15-2011, 01:23 PM
  3. functional derivation better?
    By CodeMonkey in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2007, 12:57 AM
  4. Functional programming languages... r they really functional?
    By code_mutant in forum C++ Programming
    Replies: 10
    Last Post: 02-25-2004, 05:29 AM
  5. functional dependencies
    By DMaxJ in forum C++ Programming
    Replies: 10
    Last Post: 10-23-2002, 07:07 AM