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 8437779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:31:43+00:00 2026-06-10T07:31:43+00:00

The fallowing code works with gcc 4.7. The idea is i have these generic

  • 0

The fallowing code works with gcc 4.7. The idea is i have these generic functions, which work on sequences, pointers, tupples, pairs, user-defined types, and whatnot. If one of these functions is defined for a type, then all should be. The problem i’m having is determining how to specialize them. I decided to define a template class that would be specialized for each type, implementing each function, and then a free function that just forwards to the in-class implementation.

#include <utility>
#include <vector>
#include <iterator>
#include <memory>
#include <iostream>
#include <algorithm>

using namespace std;

template< class M > struct Mon;

template< class X, class F, 
          class M = Mon< typename decay<X>::type > > 
auto mon( X&& x, F f ) -> decltype( M::mon(declval<X>(),f) ) {
    return M::mon( forward<X>(x), f );
}

template< class C > struct IsSeqImpl {
    // Can only be supported on STL-like sequence types, not pointers.
    template< class _C > static true_type f(typename _C::iterator*);
    template< class _C > static false_type f(...);

    typedef decltype( f<C>(0) ) type;
};

template< class C > struct IsSeq : public IsSeqImpl<C>::type { };

/* Enable if is an STL-like sequence. */
template< class C, class R > struct ESeq : std::enable_if<IsSeq<C>::value,R> { };

template< class Seq > 
struct Mon : ESeq< Seq, Seq >::type
{
    template< class S, class F >
    static S mon( const S& s, F f ) {
        S r;
        transform( begin(s), end(s), back_inserter(r), f );
        return r;
    }
};

template< class P > struct IsPtrImpl {
    template< class X > static true_type  f( X );
    template< class X > static false_type f( ... );

    typedef decltype( f(*declval<P>()) ) type;
};

template< class P > struct IsPtr : public IsPtrImpl<P>::type { };

template< class P, class R > struct EPtr : enable_if<IsPtr<P>::value,R> { };

template< class X > struct Mon< X* >
{
    template< class F, class R = decltype( declval<F>()(declval<X>()) ) >
    static unique_ptr<R> mon( X* x, F f ) {
        typedef unique_ptr<R> U;
        return x ? U( new R(f(*x)) ) : U(nullptr);
    }
};

int add_one( int x ) { return x + 1; }

int main()
{
    vector<int> v = {1,2,3,4,5};    
    int x = 5;

    auto v2 = mon( v, add_one );
    auto x2 = mon( &x, add_one );

    // Should print 2 and 6.
    cout << v2[0] << '\n';
    cout << *x2 << '\n';
}

What i’d like to do is specialize Mon for more generic types but when i try to use the enable_if inheritance trick again, gcc complains Mon is already defined. I’ve also tried the technique of making the second template argument a true_ or false_type for SFINAE as mentioned in this question, but had no luck in getting it to compile.

Ideally, whenever i think of a category of types i want to define an action for, i should be able to write an enable_if and write the entire group of functions in a template specialization. This saves the trouble of writing one enable_if per function. Pessimistically, i’d have to specialize the group for every plausible type in each category in order to really be generic.

Can i possibly write this in a generic and extensible way?

PS: If only concepts were a part of C++11.

  • 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-10T07:31:45+00:00Added an answer on June 10, 2026 at 7:31 am

    Caution: I am afraid I did not understood the question fully, so I may be out of line…

    As far as I understand, you are trying to solve a compile-time dispatch issue, basically, the structure you would like is:

    • a generic function foo
    • a number of specializations that work for a given type (or a given family of type)

    And the issue you have is both that:

    • you want as many foo_impl as you’d like, and adding new types should not impact the existing ones
    • you need to select the right foo_impl.

    Does not it sound like simple function ?

    This can be done, relatively simply, by using traits.

    void foo_tag_detection(...) {} // void cannot be passed down as an argument
                                   // so if this is picked up we are guaranteed
                                   // a compile-time error
    
    template <typename T>
    void foo(T const& t) {
        foo_impl(t, foo_tag_detection(t));
    }
    

    Then we create a tag specific to our types, here for sequences

    // For sequence we'll use a "sequence_tag", it can be reused for other functions
    struct sequence_tag {};
    

    And implement the sequence detection, as well as the overload of foo_impl we need

    template <typename Seq>
    auto foo_tag_detection(Seq const& s) -> decltype(s.begin(), s.end(), sequence_tag{}) {
        return sequence_tag{};
    }
    
    template <typename Seq>
    void foo_impl(Seq const& s, sequence_tag) { ... }
    

    Note that used decltype here to trigger SFINAE depending on the operations I needed s to support; it’s a really powerful mechanism, and surprisingly terse.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code (works only on gcc): #include <iostream> #include <cstdlib> #include
I have the following code which works on the compilers I have available (xlC
The following code works with Visual Studio 2008 but not with GCC/G++ 4.3.4 20090804.
The following code works under gcc versions 2.9 through 4.4 but not version 4.5:
The following example code compiles under gcc and works as I would hope. It
The following code works with GCC's C compiler, but not with the C++ compiler.
I have some code that uses some shared libraries (c code on gcc). When
Empirically the following works (gcc and VC++), but is it valid and portable code?
The situation: I have a piece of code that works when compiled for 32-bits
The following code works fine on Linux but throws an exception on OS X

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.