Thread: how to write variadic template for variadic function?

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    how to write variadic template for variadic function?

    consider the following class:

    Code:
    template <class T>
    struct Stub {
    
    T& m_Function;
    
    template <class Args...>
    bool doesItThrow (Args... args){
       try{ m_Function(args); }
       catch(std::exception& e){ return true; }
       return false;
    }
    
    }
    (let's say T is always instance of std::function)
    I can't configure doesItThrows's template decleration to make it work..
    I want the argument to be passed like the arguments are passed in std::thread

    any help?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Declare doesItThrow as

    Code:
    template <typename... Args>
    bool doesItThrow (Args... args)
    and when you call m_Function, call it as

    Code:
    m_Function(args...);
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    For reference, see this wikipedia article.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It really should be

    Code:
    template <typename... Args>
    bool doesItThrow(Args&&... args)
    {
        try { m_Function(std::forward<Args>(args)...); }
        catch (std::exception&) { return true; }
        return false;
    }
    Look up perfect forwarding.
    But really, you should be looking at the noexcept specifier:

    Code:
    void foo1() noexcept {}
    void foo2() {}
    
    void do_something(std::true_type) {} // If it was true
    void do_something(std::false_type) {} // if it was false
    
    int main()
    {
    	do_something(std::integral_constant<bool, noexcept(foo1())>()); // Do static type dispatch
    	std::cout << "foo1: " << noexcept(foo2()) << "\n"; // Do print out boolean value (can be used in if statements)
    	return 0;
    }
    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. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    thank you both. work like a charm .
    C++ is just awesome

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing variadic references to template function
    By milli-961227 in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2014, 12:43 PM
  2. Variadic Template Basic Syntax
    By Grumpulus in forum C++ Programming
    Replies: 3
    Last Post: 08-01-2014, 12:39 PM
  3. variadic template string formatting
    By Elkvis in forum C++ Programming
    Replies: 0
    Last Post: 09-14-2011, 03:54 PM
  4. Variadic function
    By Richardcavell in forum C Programming
    Replies: 17
    Last Post: 03-01-2011, 01:34 AM
  5. C++0x: Variadic template of the same type
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 03-31-2010, 01:32 PM