CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2005
    Posts
    382

    global constants - two alternatives...

    Given:

    Code:
    # include <iostream>
    
    //In a file called ... global_constants.h 
    class global_constants1 
    {
    public :
      static const int dummy         = 5; 
      static const double  dprec_value1  ; 
    };
    //global_constants.h or global_constants.cpp
    const double global_constants1::dprec_value1 = 4.3 ;
    
    
    //In a file called ... global_constants.h 
    namespace global_constants2 
    {
      const int    dummy        = 5; 
      double const dprec_value1 = 4.3 ;
    }
    
    int main()
    {
      std::cout << global_constants2::dummy << std::endl;
      std::cout << global_constants1::dummy << std::endl;
    
      std::cin.get(); 
    }
    The code show reflects two alternatives to global constants definition. I'm able to deduce from the standard that constants have internal linkage. That said, I think you'll get internal linkage in both versions and any decent compiler will eliminate unused symbols in translation unit that includes these files. Simply put there's no advantage of one over the other. True or False?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: global constants - two alternatives...

    If you're really concerned about unused symbols and whatnot, then why not use the preprocessor? Simple constants are one area in which #defines don't have any real downside.

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: global constants - two alternatives...

    Quote Originally Posted by Lindley View Post
    Simple constants are one area in which #defines don't have any real downside.
    Unless you want to examine their value in the debugger.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  4. #4
    Join Date
    Dec 2005
    Posts
    382

    Re: global constants - two alternatives...

    Quote Originally Posted by Lindley View Post
    If you're really concerned about unused symbols and whatnot, then why not use the preprocessor? Simple constants are one area in which #defines don't have any real downside.
    My concern goes beyond 'unused symbols'. I'm trying to make sense of a coding standard that suggests using the 'global_constants1' option.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: global constants - two alternatives...

    Quote Originally Posted by VladimirF View Post
    Unless you want to examine their value in the debugger.
    Possible, I suppose, if they're just aliased to other constants. In that case, another approach would probably be preferable. But for many cases, there's little incentive to directly examine them in the debugger since....well, they don't change from the value you assign them, by definition.

  6. #6
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: global constants - two alternatives...

    Quote Originally Posted by Lindley View Post
    If you're really concerned about unused symbols and whatnot, then why not use the preprocessor? Simple constants are one area in which #defines don't have any real downside.
    #define-s ignore namespaces. I try to avoid them as much as possible.
    My hobby projects:
    www.rclsoftware.org.uk

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: global constants - two alternatives...

    That's a good point, particularly when defining a library rather than an end program.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured