Is template without function parameter list OK?

Discussion in 'C++' started by Nephi Immortal, Oct 8, 2011.

  1. I use template to replace assert macro. I don’t have to provide
    function parameter list if I want. I use Static_Data class to access
    data members through template parameter.
    If you set ASSERT to false in either debug mode or release mode, the
    code is omitted from inserting into main function.
    Is my code OK and readable? I need to change some names to describe
    good readability.

    static const bool ASSERT = false;

    struct Static_Data
    {
    static const char a = 1;
    static const char b = 2;
    static const char c = 3;
    };

    template< typename t_Static_Data >
    bool Do()
    {
    printf( "Assert Message: %d %d %d\n", t_Static_Data::a,
    t_Static_Data::b, t_Static_Data::c );
    return true;
    }

    int main()
    {
    ASSERT && Do< Static_Data >();

    return 0;

    }
    Nephi Immortal, Oct 8, 2011
    #1
    1. Advertising

  2. On 10/7/2011 8:12 PM, Nephi Immortal wrote:
    > I use template to replace assert macro. I don’t have to provide
    > function parameter list if I want. I use Static_Data class to access
    > data members through template parameter.
    > If you set ASSERT to false in either debug mode or release mode, the
    > code is omitted from inserting into main function.
    > Is my code OK and readable? I need to change some names to describe
    > good readability.


    I would change the name of your macro. ASSERT what? IOW, what is it
    that you're asserting? It's possible that what you want is a macro that
    wraps your 'ASSERT' and the function call, something like

    #define IF_ASSERT_CALL(f) ASSERT && f

    and use it

    IF_ASSERT_CALL(Do<Static_Data>());

    (again, the word "ASSERT" seems out of place, perhaps you want to
    replace it with something better describing the fact that it's a global
    flag of sorts.

    const bool MY_CODE_INSERTION_FLAG = false; // no need for 'static'

    >
    > static const bool ASSERT = false;
    >
    > struct Static_Data
    > {
    > static const char a = 1;
    > static const char b = 2;
    > static const char c = 3;
    > };
    >
    > template< typename t_Static_Data>
    > bool Do()
    > {
    > printf( "Assert Message: %d %d %d\n", t_Static_Data::a,
    > t_Static_Data::b, t_Static_Data::c );
    > return true;
    > }
    >
    > int main()
    > {
    > ASSERT&& Do< Static_Data>();
    >
    > return 0;
    >
    > }


    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 8, 2011
    #2
    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. sebastian
    Replies:
    1
    Views:
    278
  2. Fei Liu
    Replies:
    0
    Views:
    414
    Fei Liu
    Oct 25, 2007
  3. Fei Liu
    Replies:
    4
    Views:
    755
    Victor Bazarov
    Oct 26, 2007
  4. Stuart Redmann
    Replies:
    5
    Views:
    469
    Stuart Redmann
    Dec 14, 2007
  5. AzamSharp
    Replies:
    2
    Views:
    163
Loading...

Share This Page