templates

LNK2019 - why unresolved external with template friend function

£可爱£侵袭症+ 提交于 2021-02-19 03:37:08
问题 I don't get this error: #include <iostream> using namespace std; // LNK2019f.cpp // LNK2019 expected template<class T> void f(T) {} template<class T> struct S { friend void f(T); // try the folowing line instead // friend void f<T>(T); }; int main() { S<int> s; int a = 2; f(a); // unresolved external } Taken from http://msdn.microsoft.com/en-us/library/799kze2z(v=vs.80).aspx Why does the error not show up if I comment out S< int > s ? I got that I need to declare the template argument list as

Passing an integer or a type as a template parameter?

邮差的信 提交于 2021-02-19 03:12:58
问题 Here is an example case of what I'm trying to do (it is a "test" case just to illustrate the problem) : #include <iostream> #include <type_traits> #include <ratio> template<int Int, typename Type> constexpr Type f(const Type x) { return Int*x; } template<class Ratio, typename Type, class = typename std::enable_if<Ratio::den != 0>::type> constexpr Type f(const Type x) { return (x*Ratio::num)/Ratio::den; } template</*An int OR a type*/ Something, typename Type> constexpr Type g(const Type x) {

Passing an integer or a type as a template parameter?

冷暖自知 提交于 2021-02-19 03:12:49
问题 Here is an example case of what I'm trying to do (it is a "test" case just to illustrate the problem) : #include <iostream> #include <type_traits> #include <ratio> template<int Int, typename Type> constexpr Type f(const Type x) { return Int*x; } template<class Ratio, typename Type, class = typename std::enable_if<Ratio::den != 0>::type> constexpr Type f(const Type x) { return (x*Ratio::num)/Ratio::den; } template</*An int OR a type*/ Something, typename Type> constexpr Type g(const Type x) {

Passing an integer or a type as a template parameter?

做~自己de王妃 提交于 2021-02-19 03:10:50
问题 Here is an example case of what I'm trying to do (it is a "test" case just to illustrate the problem) : #include <iostream> #include <type_traits> #include <ratio> template<int Int, typename Type> constexpr Type f(const Type x) { return Int*x; } template<class Ratio, typename Type, class = typename std::enable_if<Ratio::den != 0>::type> constexpr Type f(const Type x) { return (x*Ratio::num)/Ratio::den; } template</*An int OR a type*/ Something, typename Type> constexpr Type g(const Type x) {

Preventing implicit conversion operator only for binary operators

走远了吗. 提交于 2021-02-19 02:22:09
问题 I'm having an issue that I've boiled down to the following, where an == operator usage compiles even though it's supposed to fail (C++17, tested on GCC 5.x, 8.x, and 9.x): template <int N> struct thing { operator const char * () const { return nullptr; } bool operator == (const thing<N> &) const { return false; } }; int main () { thing<0> a; thing<1> b; a == b; // i don't want this to compile, but it does } The reason it is compiling is because the compiler is choosing to do this: (const char

Show the dictionary key in django template

江枫思渺然 提交于 2021-02-19 02:06:36
问题 Im wondering how i could show the dictionary key itself in a django template Example dictionary: resources = {'coin': coin, 'grain': grain, 'iron': iron, 'stone': stone, 'wood': wood,} Template <b>Coin: </b>{{ upgrade.coin }} Were i want to use the dictionary key (+some html) instead of the hard coded "Coin:" Can anyone please help me out? 回答1: Use for tag with dict.items if you want to print all key/value pairs: {% for key, value in resources.items %} <b>{{ key }}: </b>{{ value }} {% endfor

Using enable_if with struct specialization

…衆ロ難τιáo~ 提交于 2021-02-19 01:16:36
问题 I am attempting to define a template that will will specify a storage type given another type T. I'd like to use enable_if to catch all the arithmetic types. Below is my attempt at this which complains the template is redeclared with 2 parameters. I tried adding a 2nd dummy parm to the primary template but get a different error. How can this be done? #include <string> #include <type_traits> template <typename T> struct storage_type; // want compile error if no match // template <typename T,

Using enable_if with struct specialization

爷,独闯天下 提交于 2021-02-19 01:15:48
问题 I am attempting to define a template that will will specify a storage type given another type T. I'd like to use enable_if to catch all the arithmetic types. Below is my attempt at this which complains the template is redeclared with 2 parameters. I tried adding a 2nd dummy parm to the primary template but get a different error. How can this be done? #include <string> #include <type_traits> template <typename T> struct storage_type; // want compile error if no match // template <typename T,

Using enable_if with struct specialization

拟墨画扇 提交于 2021-02-19 01:15:34
问题 I am attempting to define a template that will will specify a storage type given another type T. I'd like to use enable_if to catch all the arithmetic types. Below is my attempt at this which complains the template is redeclared with 2 parameters. I tried adding a 2nd dummy parm to the primary template but get a different error. How can this be done? #include <string> #include <type_traits> template <typename T> struct storage_type; // want compile error if no match // template <typename T,

Using enable_if with struct specialization

跟風遠走 提交于 2021-02-19 01:15:24
问题 I am attempting to define a template that will will specify a storage type given another type T. I'd like to use enable_if to catch all the arithmetic types. Below is my attempt at this which complains the template is redeclared with 2 parameters. I tried adding a 2nd dummy parm to the primary template but get a different error. How can this be done? #include <string> #include <type_traits> template <typename T> struct storage_type; // want compile error if no match // template <typename T,