Constness Question

closed account (S6k9GNh0)
Consider the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct a {
};
 
 
template<a const& token>
struct b {
};
 
 
a const x = a();
//a x;
 
 
typedef b<x> c;
 
 
int main() {
}


Line 10 is invalid. But line 11 is acceptable. The error given is provided in the following link: http://ideone.com/czo2H

Can anyone explain this? It seems to have stumped a majority of ##c++.
Last edited on
Interesting. On a related note, this fails as well:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct a {
};

template<const a& token>
struct b {
};

const a x = a();
const a& ref = x;

typedef b<ref> d;

int main() {
}
/Users/albatross/Documents/testpad/testpad.cpp:14: error: could not convert template argument 'ref' to 'const a&'
/Users/albatross/Documents/testpad/testpad.cpp:14: error: invalid type in declaration before ';' token


-Albatross
Last edited on
closed account (z05DSL3A)
Templates have external linkage, type and non-type arguments also have to have external linkage. Non-extern const have internal linkage, hence the error.

Try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct a {
};
 
template<a const& token>
struct b {
};
 
extern a const x = a();
//a x;
 
typedef b<x> c;
 
int main() {
}

Last edited on
Topic archived. No new replies allowed.