Using implicit function template specialization to check if an identifier is an array

Discussion in 'C++' started by Angel Tsankov, Jan 12, 2007.

  1. I'm thinking about using a function tamplate to verify that an
    identifier is of an array type. May the following template be
    used for this purpose:

    template< typename T, int N >
    void check_array( T(&)[N] )
    {
    }

    And what about using size_t instead of int?


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Angel Tsankov, Jan 12, 2007
    #1
    1. Advertising

  2. [ I dropped cross-posting in c.l.c++.m so the articles appear sooner]

    Angel Tsankov wrote:
    > I'm thinking about using a function tamplate to verify that an
    > identifier is of an array type. May the following template be
    > used for this purpose:
    >
    > template< typename T, int N >
    > void check_array( T(&)[N] )
    > {
    > }
    >
    > And what about using size_t instead of int?


    Yes, I'd probably use 'size_t', although 'int' would work almost
    as well if the size is less than INT_MAX.

    Perhaps incorporating this with a compile-time assert would be
    a bit better, and you could try incorporating a decent error
    message into it, but should work right out of the box...

    Why do you ask? Have you run into any problem with it?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 12, 2007
    #2
    1. Advertising

  3. Angel Tsankov schrieb:

    > I'm thinking about using a function tamplate to verify that an
    > identifier is of an array type. May the following template be
    > used for this purpose:
    >
    > template< typename T, int N >
    > void check_array( T(&)[N] )
    > {
    > }


    Yes, absolutely.


    > And what about using size_t instead of int?


    Indeed size_t is the canonical type for this approach, because it is
    unsigned and because every statically creatable array must have a
    size expressable via sizeof, which itself returns size_t.

    Greetings from Bremen,

    Daniel Kr├╝gler


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    =?iso-8859-1?q?Daniel_Kr=FCgler?=, Jan 12, 2007
    #3
  4. >[ I dropped cross-posting in c.l.c++.m so the articles appear
    >sooner]
    >
    > Angel Tsankov wrote:
    >> I'm thinking about using a function tamplate to verify that an
    >> identifier is of an array type. May the following template be
    >> used for this purpose:
    >>
    >> template< typename T, int N >
    >> void check_array( T(&)[N] )
    >> {
    >> }
    >>
    >> And what about using size_t instead of int?

    >
    > Yes, I'd probably use 'size_t', although 'int' would work
    > almost
    > as well if the size is less than INT_MAX.
    >
    > Perhaps incorporating this with a compile-time assert would be
    > a bit better,


    What is your idea?

    > and you could try incorporating a decent error
    > message into it, but should work right out of the box...


    Is there a standard way to incorporate an error message?

    > Why do you ask? Have you run into any problem with it?


    No, I have not found any problems with it. I am just curious if
    it is reliable, that is whether the template shall be implicitly
    instantiated if and only if the argument is of an array type.
    Angel Tsankov, Jan 12, 2007
    #4
  5. Angel Tsankov wrote:
    >> [ I dropped cross-posting in c.l.c++.m so the articles appear
    >> sooner]
    >>
    >> Angel Tsankov wrote:
    >>> I'm thinking about using a function tamplate to verify that an
    >>> identifier is of an array type. May the following template be
    >>> used for this purpose:
    >>>
    >>> template< typename T, int N >
    >>> void check_array( T(&)[N] )
    >>> {
    >>> }
    >>>
    >>> And what about using size_t instead of int?

    >>
    >> Yes, I'd probably use 'size_t', although 'int' would work
    >> almost
    >> as well if the size is less than INT_MAX.
    >>
    >> Perhaps incorporating this with a compile-time assert would be
    >> a bit better,

    >
    > What is your idea?


    Check out Boost's compile-time assertions based on templates.
    They have a whole bunch of them, IIRC.

    >> and you could try incorporating a decent error
    >> message into it, but should work right out of the box...

    >
    > Is there a standard way to incorporate an error message?


    No. And it's not a simple task either. If you call your function
    'check_array', and it fails, what error message do you see? Would
    naming 'check_array' differently clarify it? You could try using
    SFINAE mechanism to even cause something like "not an array" from
    your particular compiler, I bet.

    >> Why do you ask? Have you run into any problem with it?

    >
    > No, I have not found any problems with it. I am just curious if
    > it is reliable, that is whether the template shall be implicitly
    > instantiated if and only if the argument is of an array type.


    Yes, that's certaintly so.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 12, 2007
    #5
  6. Re: Using implicit function template specialization to check if anidentifier is an array

    Angel Tsankov wrote:
    > I'm thinking about using a function tamplate to verify that an
    > identifier is of an array type. May the following template be used for
    > this purpose:
    >
    > template< typename T, int N >
    > void check_array( T(&)[N] )
    > {
    > }


    The only change I would consider is making it so it can be used statically.

    e.g.

    template< typename T, unsigned N >
    char (check_array_helper( T(&)[N] ))[N];

    #define check_array(X) (sizeof check_array_helper(X) )


    Perhaps you could eliminate the marco using somthing like this.

    template< typename T>
    struct check_array;

    template< typename T, unsigned N >
    struct check_array< T(&)[N] >
    {
    static const bool is_array = true;
    };


    Now you can use somthing like this:
    check_array<var>::is_array



    >
    > And what about using size_t instead of int?


    Use size_t.
    Gianni Mariani, Jan 12, 2007
    #6
  7. >> I'm thinking about using a function tamplate to verify that an
    >> identifier is of an array type. May the following template be
    >> used for this purpose:
    >>
    >> template< typename T, int N >
    >> void check_array( T(&)[N] )
    >> {
    >> }

    >
    > The only change I would consider is making it so it can be used
    > statically.
    >
    > e.g.
    >
    > template< typename T, unsigned N >
    > char (check_array_helper( T(&)[N] ))[N];
    >
    > #define check_array(X) (sizeof check_array_helper(X) )
    >
    >
    > Perhaps you could eliminate the marco using somthing like this.
    >
    > template< typename T>
    > struct check_array;
    >
    > template< typename T, unsigned N >
    > struct check_array< T(&)[N] >
    > {
    > static const bool is_array = true;
    > };
    >
    >
    > Now you can use somthing like this:
    > check_array<var>::is_array


    Well, one may only specify a typename or an integer as a template
    argument. In fact, this is the basic difference between class
    templates and function templates - function template parameters
    may be deduced from the arguments to the function. Class template
    parameters however, may not be deduced.
    Angel Tsankov, Jan 13, 2007
    #7
  8. Angel Tsankov

    Greg Guest

    Angel Tsankov wrote:
    > I'm thinking about using a function tamplate to verify that an
    > identifier is of an array type. May the following template be
    > used for this purpose:
    >
    > template< typename T, int N >
    > void check_array( T(&)[N] )
    > {
    > }


    How do you envision using the check_array() function template to a test
    for an array type exactly? If anything, std::tr1::is_array (or
    std::is_array in the future) is pretty easy to use:

    #include <tr1/type_traits>

    char a[30];

    using std::tr1::is_array;

    static bool b = is_array<a>::value;

    Greg
    Greg, Jan 13, 2007
    #8
  9. Re: Using implicit function template specialization to check if anidentifier is an array

    Angel Tsankov wrote:
    ....
    >> Now you can use somthing like this:
    >> check_array<var>::is_array

    >
    > Well, one may only specify a typename or an integer as a template
    > argument. In fact, this is the basic difference between class templates
    > and function templates - function template parameters may be deduced
    > from the arguments to the function. Class template parameters however,
    > may not be deduced.


    Yep - I don't know what I was thinking...
    Gianni Mariani, Jan 13, 2007
    #9
  10. Re: Using implicit function template specialization to check if anidentifier is an array

    Greg wrote:
    > Angel Tsankov wrote:
    >> I'm thinking about using a function tamplate to verify that an
    >> identifier is of an array type. May the following template be
    >> used for this purpose:
    >>
    >> template< typename T, int N >
    >> void check_array( T(&)[N] )
    >> {
    >> }

    >
    > How do you envision using the check_array() function template to a test
    > for an array type exactly? If anything, std::tr1::is_array (or
    > std::is_array in the future) is pretty easy to use:
    >
    > #include <tr1/type_traits>
    >
    > char a[30];
    >
    > using std::tr1::is_array;
    >
    > static bool b = is_array<a>::value;
    >
    > Greg
    >


    Only works for variables with external linkage.
    Gianni Mariani, Jan 13, 2007
    #10
  11. >>> I'm thinking about using a function tamplate to verify that
    >>> an
    >>> identifier is of an array type. May the following template be
    >>> used for this purpose:
    >>>
    >>> template< typename T, int N >
    >>> void check_array( T(&)[N] )
    >>> {
    >>> }

    >>
    >> How do you envision using the check_array() function template
    >> to a test
    >> for an array type exactly? If anything, std::tr1::is_array (or
    >> std::is_array in the future) is pretty easy to use:


    Where can I get an implementation of tr1?
    Angel Tsankov, Jan 13, 2007
    #11
  12. Angel Tsankov

    Greg Guest

    Angel Tsankov wrote:
    > I'm thinking about using a function tamplate to verify that an
    > identifier is of an array type. May the following template be
    > used for this purpose:
    >
    > template< typename T, int N >
    > void check_array( T(&)[N] )
    > {
    > }


    How do you envision using the check_array function template to test for
    an array type? I think std::tr1::is_array would probably be more
    convenient:

    #include <iostream>
    #include <ios>
    #include <tr1/type_traits>

    using std::tr1::is_array;

    int main()
    {
    bool b = is_array< int[5] >::value;

    std::cout << std::boolalpha << b << "\n";
    }

    Program Output:

    true

    Greg
    Greg, Jan 13, 2007
    #12
  13. >> I'm thinking about using a function tamplate to verify that an
    >> identifier is of an array type. May the following template be
    >> used for this purpose:
    >>
    >> template< typename T, int N >
    >> void check_array( T(&)[N] )
    >> {
    >> }

    >
    > How do you envision using the check_array function template to
    > test for
    > an array type? I think std::tr1::is_array would probably be
    > more
    > convenient:
    >
    > #include <iostream>
    > #include <ios>
    > #include <tr1/type_traits>
    >
    > using std::tr1::is_array;
    >
    > int main()
    > {
    > bool b = is_array< int[5] >::value;
    >
    > std::cout << std::boolalpha << b << "\n";
    > }
    >
    > Program Output:
    >
    > true


    Can one use std::tr1::is_array with array, e.g.:

    int array[5];
    bool const b = std::tr1::is_array<array>::value;
    Angel Tsankov, Jan 14, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kobu
    Replies:
    7
    Views:
    461
  2. Guest
    Replies:
    1
    Views:
    485
    persenaama
    Sep 18, 2005
  3. Replies:
    2
    Views:
    1,772
  4. Replies:
    2
    Views:
    905
    Owen Jacobson
    Dec 11, 2007
  5. S_K
    Replies:
    0
    Views:
    662
Loading...

Share This Page