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

  • Home
  • SEARCH
  • 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 6370649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:57:27+00:00 2026-05-25T00:57:27+00:00

I was trying to make a simplified version of my code for this question

  • 0

I was trying to make a simplified version of my code for this question. In the process I encountered something that does not make sense to me. Can someone explain why I obtain the following result? Here is the code, it should compile and run in gcc.

#include <string>
#include <iostream>
#include <typeinfo>

using std::string;

template <typename T> inline char getType(const T&) {return 'S'; }
inline char getType(const char&) {return 'C';}
template <typename T> inline char getType(T*) {return 'A'; }

template <typename T> inline void writeData(const T& x) { 
    printf("Calling default writeData...\n");
    char type = getType(x);
    if (type == 'S') {
        printf("ERROR: binaryWrite::writeData ->  Structure not defined.\n");
        exit(1);
    }
    std::cout << x << std::endl;
}

template <typename T> inline void writeData(T* x, const unsigned int& len) { 
    printf("Writing array with writeData...\n");
    char type = getType(x[0]);
    std::cout << len << std::endl;
    if (type == 'S') {
        for (int i=0; i < len; ++i) {
            writeData(x[i]);
        }
    } else {
        for (int i=0; i < len; ++i) {
            std::cout << x[i] << std::endl;
        }
    }   
}

class binaryWrite {
public:
    binaryWrite(void) {}
    template <typename T> 
    void write(const T& x, const char* name) { 
        writeData(x);
    }
};

inline void writeData(const string& s) {
    unsigned int len = s.size();
    const char* pt = s.c_str();
    writeData(pt, len);
}

int main () {
    string str = "Hello World";
    writeData(str);
    binaryWrite BF;
    BF.write(str, "str");
    return 0;
}

This is the output:

manuel-lopezs-macbook-pro:binaryFiles jmlopez$ g++ -o example example.cpp
manuel-lopezs-macbook-pro:binaryFiles jmlopez$ ./example
Writing array with writeData...
11
H
e
l
l
o

W
o
r
l
d
Calling default writeData...
ERROR: binaryWrite::writeData ->  Structure not defined.

First it calls writeData with the string version. After I try calling that version with the binaryWrite function write (This function calls writeData) it calls the function defined by the template.

I played around with it and found out that if I move the overloaded function for the string right above the class definition of binaryWrite then I get the result that I want.

This is the change:

inline void writeData(const string& s) {
    unsigned int len = s.size();
    const char* pt = s.c_str();
    writeData(pt, len);
}

class binaryWrite {
public:
    binaryWrite(void) {}
    template <typename T> 
    void write(const T& x, const char* name) { 
        writeData(x);
    }
};

This is the output

Writing array with writeData...
11
H
e
l
l
o

W
o
r
l
d
Writing array with writeData...
11
H
e
l
l
o

W
o
r
l
d

It would seem as if binaryWrite did not know about the overloaded function writeData for string in the first case. But after the switch, since I defined the overloaded function first then it knows. Is this the right explanation?

What I would like to do eventually is use the macro WRITESTRUCT for other types but this definition would be in some other files so I won’t be able to write them before the definition of binaryWrite. Any ideas how to overcome this problem if my explanation is indeed the right one?

  • 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-25T00:57:28+00:00Added an answer on May 25, 2026 at 12:57 am

    Okay, playing around with your code, I found it also performs correctly if you move ALL of the writeData templates and overload below the binaryWrite class definition.

    When the compiler encounters binaryWrite::write, it checks to see if it has a definition for writeData at that point. It’s selecting template <typename T> inline void writeData(const T& x) at that point because it only has access to the first two templates. It then never goes back to see if there is a better choice.

    When you move the writeData templates after binaryWrite, the compiler has no definition for writeData, and decides it will look again during template instantiation. So when you then use binaryWrite::write, it picks the direct overload, instead of the template.

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

Sidebar

Related Questions

Trying to make a make generic select control that I can dynamically add elements
I'm trying to make a PHP (5) object that can iterate through its properties,
This is a simplified version of a class that I have in my project.
The following code snippet is a simplified version of my issue. Basically I'm trying
I'm trying to make a function that can be applied to a value returned
Trying to make a custom :confirm message for a rails form that returns data
Im trying to make a function that will return an element of type point:
I'm trying to make a form that handles the checking of a domain: the
I have a fairly complex relationship that I am trying to make work with
I'm trying to create a Makefile that will download and process file a file

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.