为什么BOOST_FOREACH不能有时与C ++字符串一起工作? [英] Why does BOOST_FOREACH not work sometimes with C++ strings?

查看:159
本文介绍了为什么BOOST_FOREACH不能有时与C ++字符串一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用BOOST_FOREACH来遍历一个C ++字符串的字符串,如下所示:

I am using BOOST_FOREACH to iterate through the characters of a C++ string like this:

void foobar(const string& str)
{
    BOOST_FOREACH(const char ch, str)
    {
        // Do something with ch
    }

    return;
}

这段代码在以下编译模式下正常工作:

This piece of code works fine with the following compilation modes:


  • 多线程调试(/ MTd)

  • 多线程调试>
  • 多线程DLL(发布)(/ MD)

在此模式下:


  • 多线程调试DLL(/ MDd)

上面的代码片段没有编译错误或警告,导致我相信BOOST_FOREACH知道它在这里处理的容器。另外,将 const char ch 更改为 const char&

There are no compilation errors or warnings with the above code snippet, leading me to believe that BOOST_FOREACH knows the container it is handling here. Also, changing const char ch to const char& ch has no change in the behaviour.

为什么这段代码导致这种错误的运行时行为?

Why is this code causing this bad runtime behaviour?

为什么只有在Debug DLL模式下?

Why only in the Debug DLL mode?

这是在C ++字符串上使用BOOST_FOREACH吗?

Is this usage of BOOST_FOREACH on C++ strings wrong?

如果是,最好的解决方法是什么?

If yes, what is the best workaround for it?

(注意,我使用Visual Studio 2008和Boost 1.39。)

(Note that I am working with Visual Studio 2008 and Boost 1.39.)

推荐答案

BOOST_FOREACH在C ++字符串上的使用是绝对正确的(参见 http://www.boost.org/doc/libs/1%5F39%5F0/doc/html/foreach.html #foreach.introduction )。

看起来像

Usage of BOOST_FOREACH on C++ strings is absolutely correct (see http://www.boost.org/doc/libs/1%5F39%5F0/doc/html/foreach.html#foreach.introduction).
Looks like the issue in

  
// Do something with ch

这篇关于为什么BOOST_FOREACH不能有时与C ++字符串一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆