English 中文(简体)
是否定了:优化了图表初步化?
原标题:Is std::map table initialization optimized?

Considering the example at the end of the question, is the map object going to be created every time the function GetName() is called?
Or is the creation going to be optimized away and created as some lookup table?

#include <iostream>
#include <sstream>
#include <map>
#include <string>
#include <boost/assign/list_of.hpp>

enum abc
{
    A = 1,
    B,
    C
};

std::string GetName( const abc v )
{
    const std::map< abc, std::string > values =
        boost::assign::map_list_of( A, "A" )( B, "B" )( C, "C" );
    std::map< abc, std::string >::const_iterator it = values.find( v );
    if ( values.end() == it )
    {
        std::stringstream ss;
        ss << "invalid value (" << static_cast< int >( v ) << ")";
        return ss.str();
    }
    return it->second;
}

int main()
{
    const abc a = A;
    const abc b = B;
    const abc c = C;
    const abc d = static_cast< abc >( 123 );

    std::cout<<"a="<<GetName(a)<<std::endl;
    std::cout<<"b="<<GetName(b)<<std::endl;
    std::cout<<"c="<<GetName(c)<<std::endl;
    std::cout<<"d="<<GetName(d)<<std::endl;
}
最佳回答

无论是在形式上还是在概念上,在《罗马教廷标准》方面,《标准》将随时建立。

The rest is up to your compiler and how you support her:

Possibly the compiler can inline the call and then move deduced invariants outside to a single point of initialization.

Possibly the compiler dislikes that your function has external linkage and so does not inline it, then having a hard time seeing that invariant from other functions.

Possibly the compiler will always check a variables constness and use one-time-initialization when it can look inside and verify that boost::assign::map_list_of( A, "A" )( B, "B" )( C, "C" ) does not mutate global state.

很多因素和唯一可靠的方法是研究产生的法典。


根据援引请求:

3.7.2.3 [basic.std.auto]:

如果某一指定自动物体已初步形成或具有副作用,则该物体不得在封面结束前销毁,即使该物体似乎未使用,也不得将之作为优化予以消除,除非该物体或其复制件可按规定予以消除。”

这基本上意味着它要么具有副作用,在这种情况下,它就被消灭了,要么是 has,在这种情况下,在C++内部几乎无法观察到;这意味着:

<>已观察到的行为始终,只要每次都叫

换言之: 不能保证初始化仅仅在自动储存之后发生,因此永远不会假定相反的<>。

问题回答

不使用ctor子?

#include <iostream>
#include <sstream>
#include <map>
#include <string>
#include <boost/assign/list_of.hpp>


enum abc
{
    A = 1,
    B,
    C
};

class LookupTable
{
    typedef std::map<abc, std::string> map_type;

public:
    LookupTable(const map_type &values)
    : table(values)
    {}

    std::string operator()(abc v) const
    {
         map_type::const_iterator it = table.find(v);
         if (table.end() == it)
         {
             std::stringstream ss;
            ss << "invalid value (" << static_cast< int >( v ) << ")";
            return ss.str();
         }
         return it->second;
    }

private:
    const map_type table;
};


int main()
{
    std::map<abc, std::string> values = boost::assign::map_list_of( A, "A" )( B, "B" )( C, "C" );
    LookupTable GetName(values);

    const abc a = A;
    const abc b = B;
    const abc c = C;
    const abc d = static_cast< abc >( 123 );

    std::cout<<"a="<<GetName(a)<<std::endl;
    std::cout<<"b="<<GetName(b)<<std::endl;
    std::cout<<"c="<<GetName(c)<<std::endl;
    std::cout<<"d="<<GetName(d)<<std::endl;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?