Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 859887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:44:18+00:00 2026-05-15T08:44:18+00:00

I’m writing a logging class that uses a templatized operator<< function. I’m specializing the

  • 0

I’m writing a logging class that uses a templatized operator<< function. I’m specializing the template function on wide-character string so that I can do some wide-to-narrow translation before writing the log message. I can’t get TCHAR to work properly – it doesn’t use the specialization. Ideas?

Here’s the pertinent code:

// Log.h header
class Log
{
  public:
    template <typename T> Log& operator<<( const T& x );

    template <typename T> Log& operator<<( const T* x );

    template <typename T> Log& operator<<( const T*& x );

    ... 
}

template <typename T> Log& Log::operator<<( const T& input )
{ printf("ref"); }

template <typename T> Log& Log::operator<<( const T* input )
{ printf("ptr"); }

template <> Log& Log::operator<<( const std::wstring& input );
template <> Log& Log::operator<<( const wchar_t* input );

And the source file

// Log.cpp 
template <> Log& Log::operator<<( const std::wstring& input )
{ printf("wstring ref"); }
template <> Log& Log::operator<<( const wchar_t* input )
{ printf("wchar_t ptr"); }
template <> Log& Log::operator<<( const TCHAR*& input )
{ printf("tchar ptr ref"); }

Now, I use the following test program to exercise these functions

// main.cpp - test program
int main()
{
 Log log;
 log << "test 1";
 log << L"test 2";
 std::string test3( "test3" );
 log << test3;
 std::wstring test4( L"test4" );
 log << test4;
 TCHAR* test5 = L"test5";
 log << test5;
}

Running the above tests reveals the following:

// Test results
ptr
wchar_t ptr
ref
wstring ref
ref

Unfortunately, that’s not quite right. I’d really like the last one to be “TCHAR”, so that I can convert it. According to Visual Studio’s debugger, the when I step in to the function being called in test 5, the type is wchar_t*& – but it’s not calling the appropriate specialization. Ideas?

I’m not sure if it’s pertinent or not, but this is on a Windows CE 5.0 device.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:44:18+00:00Added an answer on May 15, 2026 at 8:44 am

    A-HA!

    So, I broke the problem down into it’s smallest part and discovered something about the order in which template functions are matched. First, I broke the program down into this:

    // main.cpp
    int main()
    {
     Log log;
     wchar_t* test = L"test";
     log << test;
     return 0;
    }
    

    And
    // Log.h

    class Log
    {
      public:
       template <typename T> Log& operator<<( const T* x );
    };
    
    template <> Log& Log::operator<<( wchar_t* const & input );
    

    And
    // Log.ccp

    #include "Log.h"
    
    template <> Log& Log::operator<<( const wchar_t* input )
    { printf("wchar_t ptr\n"); return *this; }
    

    And sure enough, my template specialization got called. When I added another specialization to the Log header like this

    template <typename T> Log& operator<<( const T& x );
    

    The compiler started matching the new function instead. This time, however, I didn’t include a definition for the template, so the linker complained. The linker showed the following error:

    error LNK2019: unresolved external symbol "public: class Log & __thiscall Log::operator<<<wchar_t *>(wchar_t * const &)" (??$?6PA_W@Log@@QAEAAV0@ABQA_W@Z) referenced in function _main
    

    This told me the type it was trying to match:

    wchar_t* const &
    

    A const pointer, not a pointer-to-const reference! So, now my program works. I just specialize the template like so:

    template <> Log& Log::operator<<( wchar_t* const & input );
    

    Thanks for the help everyone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 433k
  • Answers 433k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'd recommend choosing one that you have an interest in,… May 15, 2026 at 2:56 pm
  • Editorial Team
    Editorial Team added an answer Possibly an answer raising more questions than actual answers, but… May 15, 2026 at 2:56 pm
  • Editorial Team
    Editorial Team added an answer If the property is a straight get/set its compiled away… May 15, 2026 at 2:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.