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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:35:37+00:00 2026-06-14T22:35:37+00:00

I play around with template specialization and SFINAE. As for the following example, the

  • 0

I play around with template specialization and SFINAE.

As for the following example, the things seems easy:

    template <class T>
    void Do(T t, typename std::enable_if<std::is_integral<T>::value >::type* = 0)
    {
        cout << "is integer" << endl;
    }

    template <class T>
    void Do(T t, typename std::enable_if<std::is_floating_point<T>::value >::type* = 0)
    {
        cout << "is float" << endl;
    }

No I tried std::is_array, but the specialization with std::is_array is never used.

So I tried out why is_array never matches:

    template <int num>
    void Do( int a[num])
    {
        cout << "int array size " << num << endl;
    }

    void Do( int* x)
    {
        cout << "int*" << endl;
    }

    ...
    int e[] = { 1,2,3 };
    Do(e);
    ...

The first mystery for me is, that the specialization with “int a[num]” did never catch! The function parameter always has the type int*.

If I use reference types I got the “correct” result:

    template <int num>
    void Do( int (&a)[num])
    {
        cout << "int array size " << num << endl;
    }

    void Do( int* &x)
    {
        cout << "int*" << endl;
    }

So my question comes up: Is there a reasonable usage of std::is_array in combination with template function parameters? I know that

    cout << boolalpha << std::is_array<decltype(e)>::value << endl;

will give me the correct result. But declaring the template selection manually gives me no functional add on. I there any way to detect (with or without SFINAE) that an template specialization from function parameters fits to an array?

  • 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-14T22:35:39+00:00Added an answer on June 14, 2026 at 10:35 pm

    I think you got it yourself – pass arrays to template functions by reference, if you want to use their type in the secialization.

    The reason you want to do this is array-to-pointer decay, which is one of the few implicit conversions that happen to template function arguments before they are matched to the parameter types. That’s why T was a pointer when you tried to check that it is an array type in DoIt. However, array-to-pointer decay does not happen when the target type is reference type. So, to sum up:

    template <class T>
    void Do(T& t, typename std::enable_if<std::is_array<T>::value >::type* = 0)
    

    should work.

    BTW the boring way of not using SFINAE

    template <class T, unsigned N>
    void Do(T (&t)[N])
    

    works too.

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

Sidebar

Related Questions

I am following the guide here to play around with the facebook php sdk,
I was playing around with class templates and statics and saw this: template<int I>
I play around with java.math.BigInteger. Here is my java class, public class BigIntegerTest{ public
I am trying to play around with mocha running on node under Windows. I've
Just starting to play around with Flask on a local server and I'm noticing
I just starting to play around with iOS and web services. I'm writing a
I'd like to play around with writing some very small, simple web-based games (checkers,
I'd like to play around with building a recommendations system, and by that I
I'm beginning to play around with MongoDB and I'm wondering if it's possible to
I'm trying to play around with some music clustering algorithms, and I thought that

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.