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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:42:06+00:00 2026-06-13T20:42:06+00:00

If I have a template container, I can use a typedef to let me

  • 0

If I have a template container, I can use a typedef to let me lookup the type at compile time:

template <typename T>
struct MyList {
    typedef T Type;
    T get_front() const;
    // ...
};

MyList<char> char_list;
MyList<char>::Type front = char_list.get_front();

In this case you could declare char front = char_list.get_front(); instead, but sometimes this can be useful (e.g. template classes that contain other template classes).

In my case, the template doesn’t specify a typename, but instead specifies an int (actually a std::size_t):

template <std::size_t N>
struct MyClass {
    // ...
};

Is there an equivalent of a typedef that I can declare inside the class that will allow me to get the value N outside of the class? Is this an appropriate place to use a static constant?

  • 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-13T20:42:08+00:00Added an answer on June 13, 2026 at 8:42 pm

    The usual approach on modern compilers is:

    static const std::size_t value = N;
    

    Note that this will raise undefined-behavior if someone tries to take its address. This often means that the address of value could result in different locations for different translation units, but its undefined-behavior nevertheless.

    Another approach usually used on older compilers, and which does not leave the door of undefined-behavior open, is emulation via enums:

    enum { value = N };
    

    Boost.Config offers the BOOST_STATIC_CONSTANT macro which will resolve to one or the other of the described approaches, based on compiler conformance. Its usage is:

    BOOST_STATIC_CONSTANT( std::size_t, value = N );
    

    It’s reference can be found at http://www.boost.org/doc/libs/1_51_0/libs/config/doc/html/boost_config/boost_macro_reference.html#boost_config.boost_macro_reference.boost_helper_macros

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

Sidebar

Related Questions

I have a boost multi index container thus. using namespace boost::multi_index; template < typename
Assume I have a template (called ExampleTemplate) that takes two arguments: a container type
Let's suppose that I have a class container: template<class T, int size> class container
I have a templated container class that looks something like this: template <class ItemType>
I'm writing an own container class and have run into a problem I can't
Is it possible to have a C++ template function which can access different fields
I have a functor which i want to use with sort() the container in
I want to do something like this: template<typename CType, unsigned int targetDimensions> struct GeneratePointerType
When using a container class like vector , list , etc., I can use
I have my templated container class that looks like this: template< class KeyType, class

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.