Wednesday, October 15, 2008

String As Template Parameter

Consider the following template function :-


template<const char* T>
void fun()
{
}


How can we make a call to this function?

"Easy!". I heard you are saying that way. Just :-


const char* s0 = "hello world";

int main()
{

fun<s0>();
return 0;
}


However, when performing compilation, our Visual C++ gives us the following compilation error :-

error C2975: 'T' : invalid template argument for 'fun', expected compile-time constant expression

"Arghh, I know that, just provide array of character, as its length is known during compile-time."

(Note : To understand the difference among char pointer and char array, please refer to http://support.microsoft.com/kb/44463. We may use sizeof operator, which is evaluated during compile-time, to retrieve string length information from char array, but not char pointer)

So, we would have something like this :-


const char s0[] = "hello world";

int main()
{

fun<s0>();
return 0;
}


OK. It should compiled then, isn't it? However, our compiler again make a complain :-

error C2970: 'fun' : template parameter 'T' : 's0' : an expression involving objects with internal linkage cannot be used as a non-type argument

I heard you are crying.

Take a rest for a while. Let's forget string template parameter at this moment. Let us take a look into internal linkage and external linkage. Consider the following two files :-


// Module0.cpp
////////////////////
//Internal Linkage//
////////////////////

// Constant pointer which is having internal linkage
char* const s0 = "hello";

// Constant string with constant pointer is haing internal linkage
const char* const s1 = "hello";

// Internal linkage
const char s3[] = "hello";


and


// Module1.cpp
////////////////////
//Internal Linkage//
////////////////////

// Constant pointer which is having internal linkage
char* const s0 = "hello";

// Constant string with constant pointer is haing internal linkage
const char* const s1 = "hello";

// Internal linkage
const char s3[] = "hello";


There are two modules. Two modules are having the same set of variable names. However, when we compile and link them together, everything just work fine. This is because they are having internal linkage. The variables are only visible within their own translator unit (object file). No conflict will occur.

How about external linkage? Consider the following two files again.


// Module0.cpp
////////////////////
//External Linkage//
////////////////////

// Constant string which is having external linkage (Early detected during compiled time)
const char* s0 = "hello";

// String with external linkage
char* s1 = "hello";


and


// Module1.cpp
////////////////////
//External Linkage//
////////////////////

// Constant string which is having external linkage
const char* s0 = "hello";

// String with external linkage
char* s1 = "hello";


We will fail during linking time. The external linkage variables are visible among other translator units. Hence, compiler will complain duplicated variable's names being detected.

Till now, I guess you roughly have an idea on why the char array is not being accepted by template function. char array is having internal linkage by default. We need to explicit state that we want external linkage.

Please do not ask me why it must be external linkage. Ask Bjarne Stroustrup. His 14.3.2 "The C++ Programming Language" states that way.

The final piece of code to solve this problem is :-


extern const char s0[] = "hello world";

int main()
{

fun<s0>();
return 0;
}

6 comments:

Anonymous said...

I was considering buying an Iphone soon but I am questioning it because of the data plan. Right now we have the $60.00 plus $10 for extra line and $30 unlimited texting for both lines($20+$10 for my mom) and the $10 data package. If I were to get an Iphone what would the average price be for the bill and would my mom have to go on a seperate bill,etc?
[url=http://unlockiphone22.com]unlock iphone[/url]

Anonymous said...

I want not agree on it. I assume nice post. Especially the title attracted me to be familiar with the sound story.

Anonymous said...

Genial fill someone in on and this post helped me alot in my college assignement. Gratefulness you for your information.

Anonymous said...

Brim over I agree but I think the collection should prepare more info then it has.

Anonymous said...

My friend and I were recently talking about how involved with technology our daily lives have become. Reading this post makes me think back to that debate we had, and just how inseparable from electronics we have all become.


I don't mean this in a bad way, of course! Societal concerns aside... I just hope that as memory becomes less expensive, the possibility of downloading our brains onto a digital medium becomes a true reality. It's a fantasy that I daydream about almost every day.


(Posted on Nintendo DS running [url=http://quizilla.teennick.com/stories/16129580/does-the-r4-or-r4i-work-with-the-new-ds]R4i[/url] DS Fling)

Josue Aragon said...

Hello friend amazing and very interesting blog I really enjoyed reading and I would like to have any update about it and i want to know if you have any blog about cougars women

Followers