main.d(61): Error: temp_[i_] isn't mutable

Ben Hanson Ben.Hanson at tfbplc.co.uk
Sat Jun 19 10:30:26 PDT 2010


Hi there,

I'm converting the following C++ function to D:

    void negate ()
    {
        const std::size_t max_chars_ = sizeof (CharT) == 1 ?
            num_chars : num_wchar_ts;
        CharT curr_char_ = sizeof (CharT) == 1 ? -128 : 0;
        string temp_;
        const CharT *curr_ = _charset.c_str ();
        const CharT *chars_end_ = curr_ + _charset.size ();

        _negated = !_negated;
        temp_.resize (max_chars_ - _charset.size ());

        CharT *ptr_ = const_cast<CharT *> (temp_.c_str ());
        std::size_t i_ = 0;

        while (curr_ < chars_end_)
        {
            while (*curr_ > curr_char_)
            {
                *ptr_ = curr_char_;
                ++ptr_;
                ++curr_char_;
                ++i_;
            }

            ++curr_char_;
            ++curr_;
            ++i_;
        }

        for (; i_ < max_chars_; ++i_)
        {
            *ptr_ = curr_char_;
            ++ptr_;
            ++curr_char_;
        }

        _charset = temp_;
    }

Here's the complete source:

module main;

import std.algorithm;
import std.string;

template regex(StringT)
{
struct basic_string_token
{
    bool _negated = false;
	StringT _charset;
	typedef typeof(StringT.init[0]) CharT;
	enum size_t MAX_CHARS = CharT.max + 1;

	this(const bool negated_, ref StringT charset_)
	{
		_negated = negated_;
		_charset = charset_;
	}

	void remove_duplicates()
	{
		_charset.sort;
		_charset = squeeze(_charset);
	}

	void normalise()
	{
		if (_charset.length == MAX_CHARS)
		{
            _negated = !_negated;
			_charset.clear();
		}
		else if (_charset.length > MAX_CHARS / 2)
		{
			negate();
		}
	}

	void negate()
	{
		CharT curr_char_ = MAX_CHARS == 256 ? 0x80 : 0;
		StringT temp_;
		size_t curr_ = 0;
		size_t end_ = _charset.length;
		size_t i_ = 0;

        _negated = !_negated;
		temp_.length = MAX_CHARS - end_;

		while (curr_ < end_)
		{
			while (_charset[curr_] > curr_char_)
			{
				temp_[i_] = curr_char_;
				++curr_char_;
				++i_;
			}

            ++curr_char_;
            ++curr_;
            ++i_;
		}

        for (; i_ < MAX_CHARS; ++i_)
        {
            temp_ ~= curr_char_;
            ++curr_char_;
        }

		_charset = temp_;
	}
};
}

int main(char[][]argv)
{
	regex!(string).basic_string_token token_;

	token_._charset = "cccbba";
	token_.remove_duplicates();
	token_.negate();
	return 0;
}

Can anyone explain the error 'main.d(61): Error: temp_[i_] isn't
mutable'? Can I use pointers instead like the C++ code? What's the
best approach for maximum efficiency in D (pointers would make the
conversion easier to, I guess).

Thanks,

Ben


More information about the Digitalmars-d mailing list