Using const_cast<LPTSTR> with LVM_INSERTCOLUMN

I'm creating a list view and need to insert some columns into it.

I think the following code is quite safe because LVM_INSERTCOLUMN does not modify the string which pszText points to:

const std::string columnHour = loadString(IDS_LOGGING_HEADER_HOUR);

LVCOLUMN column;
ZeroMemory(&column, sizeof(column));

column.mask = LVCF_TEXT | LVCF_WIDTH;
column.cx = 64;

LPARAM columParam = reinterpret_cast<LPARAM>(&column);

// Note:
// We can use const_cast here because the content of
// pszText is not modified durning LVM_INSERTCOLUMN.

// Insert logging control column 'Hour':
column.pszText = const_cast<LPTSTR>(columnHour.c_str());
myLoggingListView.sendMessage(LVM_INSERTCOLUMN, 0, columParam);

Or should I copy the text into an std::vector instead?
[921 byte] By [Zaccheus] at [2007-11-20 10:19:59]
# 1 Re: Using const_cast<LPTSTR> with LVM_INSERTCOLUMN
That is safe. What do you mean by copying to std::vector, though ?
kirants at 2007-11-9 13:32:04 >
# 2 Re: Using const_cast<LPTSTR> with LVM_INSERTCOLUMN
Cool.

I sometimes use std::vector<TCHAR> when I need to create a non-const null-terminated copy of a string:

typedef std::basic_string<TCHAR> String;

String text(TEXT("Hello World"));

// Now make a non-const copy of text:

std::vector<TCHAR> buffer;
buffer.reserve(text.length()+1);
buffer.assign(text.begin(), text.end());
buffer.push_back(TEXT('\0'));

TCHAR* nonConstText = &buffer[0];

:)
Zaccheus at 2007-11-9 13:33:02 >