Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8916643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:17:26+00:00 2026-06-15T05:17:26+00:00

I have the following smart_ptr class. template <typename T> class smart_ptr { public: //

  • 0

I have the following smart_ptr class.

template <typename T>
class smart_ptr
{
public:
    // ... removed other member functions for simplicity
    T* get() { return ptr; }

    template <typename... Args>
    decltype(T::template operator ()(Args()...)) operator ()(Args... args) const
    {
        return (*get()).operator ()(args...);
    }

private:
    T* ptr;
};

However, when I use the smart_ptr class for type T without an operator (), it fails to compile (and rightly so).

What I wanted is to use std::enable_if to enable the member function only when T has operator (). It got obscure and complex very fast when I got into doing it, as I was thinking of using SFINAE to detect if T has operator () and then combine it with std::enable_if to get what I want. However, I got lost in creating SFINAE that is capable of detecting a variadic templated operator () (i.e. NOT a duplicate of Is it possible to write a template to check for a function's existence?).

Can anyone help? Or give another (perhaps simpler) solution?

p.s. It has to work on GCC 4.5.3.

EDIT: For the sake of completeness, I’ve answered my own question with a working solution for GCC 4.5.3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:17:28+00:00Added an answer on June 15, 2026 at 5:17 am

    Finally, I’ve found a workaround for GCC 4.5.3!

    template <typename T>
    class smart_ptr
    {
    public:
        // ... removed other member functions for simplicity
        T* get() const { return ptr; }
    
    private:
        template <typename... Args>
        struct opparen_constret
        {
            typedef decltype(std::declval<T const>()(std::declval<Args>()...)) type;
        };
    
        template <typename... Args>
        struct opparen_ret
        {
            typedef decltype(std::declval<T>()(std::declval<Args>()...)) type;
        };
    
    public:
        template <typename... Args>
        typename opparen_constret<Args...>::type operator ()(Args... args) const
        {
            return (*get())(args...);
        }
    
        template <typename... Args>
        typename opparen_ret<Args...>::type operator ()(Args... args)
        {
            return (*get())(args...);
        }
    
    private:
        T* ptr;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following class template: template<typename T> struct Wrapper { T* t_;
I have the following code: namespace { class CatBondEvent { public: CatBondEvent(Date date) :
I have the following method: public object[] GetEventsByUser(DateTime start, DateTime end, string fullUrl) The
Let's suppose I have the following line in a CSV file (I removed the
I have class foo that contains a std::auto_ptr member that I would like to
I have the following code: #include <boost/smart_ptr/shared_ptr.hpp> #include <boost/numeric/interval.hpp> #include <boost/math/distributions/students_t.hpp> using namespace boost::numeric;
I have the following problem with code folding: if I have a class with
I have a class, C. C has a member variable declared as: bool markerStart;
I have the following code snippet of a small Thread class I am trying
I have the following code: #include <memory> class Foo; typedef std::tr1::shared_ptr<Foo> pFoo_t; class DoSomething

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.