views:

146

answers:

2

I'd like to do this:

template <typename T>
struct S
{
    ...
    static double something_relevant = 1.5;
};

but I can't since something_relevant is not of integral type. It doesn't depend on T, but existing code depends on it being a static member of S.

Since S is template, I cannot put the definition inside a compiled file. How do I solve this problem ?

+5  A: 

Just define it in the header:

template <typename T>
struct S
{
    static double something_relevant;
};

template <typename T>
double S<T>::something_relevant = 1.5;

Since it is part of a template, as with all templates the compiler will make sure it's only defined once.

sbi
@sbi: doesn't it violate the one definition rule ?
Alexandre C.
No, not if we're talking templates. Otherwise function templates would do so, too.
sbi
@sbi: ok, i'll accept, you're the first to answer.
Alexandre C.
@sbi: Damn! You were quicker(by some seconds I guess). :-)
Prasoon Saurav
@sbi, @Prasoon: actually Prasoon seems to be the first. But I still accept sbi's because of the comment about the ODR (which was my primary concern).
Alexandre C.
Time for me shows that @sbi was 1m06s faster than @Prasoon
Johannes Schaub - litb
@Johannes: How do you get at the exact times? For me, it just shows "1 hour ago" each. I know I can sort them by "oldest" and "newest", and it will show the exact times in three days (until then it says "2 days ago"), but I have no idea how I get at the exact times _now_.
sbi
@sbi just hover over the text :)
Johannes Schaub - litb
@Johannes Schaub -litb: Frankly speaking when I saw sbi's answer the last two lines were `template <typename T> double something_relevant<T> = 1.5;` which is syntactically incorrect(he might have missed something );-). So I decided to post my solution.
Prasoon Saurav
@Prasoon ohh haha! 5-minutes-limits ftw xD
Johannes Schaub - litb
@sbi: If you think there's something wrong in my last comment, please do mention :-)
Prasoon Saurav
@Johannes: Dammit, I'm here for a year and I didn't know that! What else am I missing? (I still remember the shame when I discovered that the two numbers that appear when I click on the number of votes aren't a bug, but a feature.) `<goes_playing>` Wow, when I hover over your name, I see your rep! I didn't know that one either. @Prasoon: No, you're right, I iteratively arrived at where it is now. (That's why I up-voted your answer, BTW.)
sbi
@sbi hey i didn't know that rep thing either! ahaha
Johannes Schaub - litb
@Johannes: Same here, rofl.
GMan
@Johannes: What happened to your hair? BTW nice hair-style :-)
Prasoon Saurav
@Prasoon, hehe thanks. it grew up =)
Johannes Schaub - litb
@Johannes: Are you John, Paul, or Ringo?
sbi
@sbi lol :) actually litb means "let it be", so it's the right corner already! xD I'm in love with "Imagine" by John, though. Nice song!
Johannes Schaub - litb
@Johannes: I figured you were going for the [Scott Meyers look.](http://www.aristeia.com/photo.html)
James McNellis
@Johannes: Ah, I always wondered what that stood for.(And, yes, Imagine is a wonderful song.) @James: That rang a bell: ["My hair style calls into immediate question all my judgements."](http://groups.google.de/group/comp.lang.c++.moderated/msg/f7e901ce6e8a86c0) (near the end of that posting)
sbi
@James Oh, i don't wanna have the Scott Meyers look really :) I wanna look like Jesse McCartney (with his old hair style. looked so hot!).
Johannes Schaub - litb
@Johannes: Heh, I've been listening to "Imagine" lately.
GMan
@GMan: Imagine that! `:)`
sbi
+8  A: 

This will work

template <typename T>
 struct S
 {

     static double something_relevant;
 };

 template<typename T>
 double S<T>::something_relevant=1.5;

Or did I miss something?

Prasoon Saurav