Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Updated: March 2009

Converts characters of type CharType used by a locale to the corresponding characters of type char in the native character set.

This method is potentially unsafe, as it relies on the caller to check that the passed values are correct. Consider using ctype::_Narrow_s instead.

char narrow(
    CharType ch, 
    char default = '\0'
) const;
const CharType* narrow(
    const CharType* first, 
    const CharType* last,
    char default, 
    char* dest
) const;


The character of type Chartype used by the locale to be converted.


The default value to be assigned by the member function to characters of type CharType that do not have counterpart characters of type char.


A pointer to the first character in the range of characters to be converted.


A pointer to the character immediately following the last character in the range of characters to be converted.


A const pointer to the first character of type char in the destination range that stores the converted range of characters.

The first member function returns the native character of type char that corresponds to the parameter character of type CharType default if not counterpart is defined.

The second member function returns a pointer to the destination range of native characters converted from characters of type CharType.

The first member function returns do_narrow(ch, default). The second member function returns do_narrow (first, last, default, dest). Only the basic source characters are guaranteed to have a unique inverse image CharType under narrow. For these basic source characters, the following invariant holds: narrow ( widenc ), 0 ) == c.


Any specialization of ctype that overrides this method must also override the ctype::_Narrow_s method.

// ctype_narrow.cpp
// compile with: /EHsc /W3
#include <locale>
#include <iostream>
using namespace std;

int main( )
   locale loc1 ( "english" );
   wchar_t *str1 = L"\x0392fhello everyone";
   char str2 [16];
   // Note:: ctype::narrow is potentially unsafe, consider
   // using ctype::_Narrow_s instead.
   bool result1 = (use_facet<ctype<wchar_t> > ( loc1 ).narrow
      ( str1, str1 + wcslen(str1), 'X', &str2[0] ) != 0);  // C4996
   str2[wcslen(str1)] = '\0';
   wcout << str1 << endl;
   cout << &str2[0] << endl;
Xhello everyone

Header: <locale>

Namespace: std


Other Resources




March 2009


Customer feedback.

© 2015 Microsoft