English 中文(简体)
与线索和模板的奇怪故障
原标题:strange failure with const pointer and templates

下面的法典没有把我编成一个想法,说明为什么:

#include <memory>
#include <iostream>
#include <map>

template <typename MapT>
inline typename MapT::mapped_type * find_element(MapT &m, const typename MapT::key_type &key)
{
  auto it = m.find(key);
  return it != m.end() ? &it->second : NULL;
}

int main() {
  std::map<std::string, double> map0;
  const std::map<std::string, double> map1 = map0;
  auto d1 = find_element(map1, "test");
  std::cout << d1 << "
";
}

我收到了:

const.cc: In instantiation of ‘typename MapT::mapped_type* find_element(MapT&, const typename MapT::key_type&) [with MapT = const std::map<std::__cxx11::basic_string<char>, double>; typename MapT::mapped_type = double; typename MapT::key_type = std::__cxx11::basic_string<char>]’:
const.cc:15:38:   required from here
const.cc:9:24: error: invalid conversion from ‘const double*’ to ‘std::map<std::__cxx11::basic_string<char>, double>::mapped_type*’ {aka ‘double*’} [-fpermissive]
    9 |   return it != m.end() ? &it->second : NULL;
      |                        ^
      |                        |
      |                        const double*
问题回答

问题在于,被引为const :map:<std:string, Double>,因为你重新通过一个综合地图。 这反过来意味着:it <>t>>t/code>被引为(来自初始设计者)std:map<const end:string, Double>:const_iterator。 因此,<代码>it->second的类型为const Double&it->secondconst Double*

由于回归类型为<代码>double*,并且由于我们不再允许从<代码>const Double*上启动double*,你发现上述错误。

这是因为你重新从并非最忙的职能中恢复过子,这应当奏效。

#include <memory>
#include <iostream>
#include <map>

template <typename MapT>
const inline typename MapT::mapped_type* find_element(MapT& m, const typename MapT::key_type& key)
{
    auto it = m.find(key);
    return it != m.end() ? &it->second : NULL;
}

int main() {
    std::map<std::string, double> map0;
    const std::map<std::string, double> map1 = map0;
    auto d1 = find_element(map1, "test");
    std::cout << d1 << "
";
}

顺从使汇编者把一切照相加,amp;it;second;二次回归,是头两倍*。





相关问题
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?

热门标签