specialization

Problem with class template specialisations

人走茶凉 提交于 2019-12-25 16:24:13
问题 I'm trying to port some code from VC9 to G++, however Ive run into a problem with template specialisations apparently not being allowed for class members. The following code is an example of these errors for the getValue specialisations of the class methods. In all cases the error is "error: explicit specialization in non-namespace scope class ... " template<typename T> T getValue(const_iterator key)const { try{return boost::lexical_cast<T>(key->second);} catch(boost::bad_lexical_cast &e) {

C++ template, static function specialization

萝らか妹 提交于 2019-12-24 16:51:36
问题 I have a syntax error with my template I would like to partial specialize a static function of my template class class.hpp template <typename Foo, size_t bar = 26> class MyClass { MyClass(); static void function(); }; #include "class.tpp" class.tpp template <typename Foo, bar> MyClass<Foo, bar>::MyClass() { } template <typename Foo> inline void MyClass<Foo, 6>::function() { // ... } template <typename Foo> inline void MyClass<Foo, 26>::function() { // ... } error: template definition of non

C++ template, static function specialization

大城市里の小女人 提交于 2019-12-24 16:51:04
问题 I have a syntax error with my template I would like to partial specialize a static function of my template class class.hpp template <typename Foo, size_t bar = 26> class MyClass { MyClass(); static void function(); }; #include "class.tpp" class.tpp template <typename Foo, bar> MyClass<Foo, bar>::MyClass() { } template <typename Foo> inline void MyClass<Foo, 6>::function() { // ... } template <typename Foo> inline void MyClass<Foo, 26>::function() { // ... } error: template definition of non

Why is my customed `::swap` function not called?

ⅰ亾dé卋堺 提交于 2019-12-23 15:27:56
问题 Here I write a code snippet to see which swap would be called, but the result is neither. Nothing is outputted. #include<iostream> class Test {}; void swap(const Test&lhs,const Test&rhs) { std::cout << "1"; } namespace std { template<> void swap(const Test&lhs, const Test&rhs) { std::cout << "2"; } /* If I remove the const specifier,then this will be called,but still not the one in global namespace,why? template<> void swap(Test&lhs, Test&rhs) { std::cout << "2"; } */ } using namespace std;

What is the Type This struct is Inheriting From?

情到浓时终转凉″ 提交于 2019-12-23 10:59:09
问题 So this example from: http://en.cppreference.com/w/cpp/utility/variant/visit declares the specialized type: template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; Which is constructed as an r-value here: std::visit(overloaded { [](auto arg) { std::cout << arg << ' '; }, [](double arg) { std::cout << std::fixed << arg << ' '; }, [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; }, }, v); I'm

What is the Type This struct is Inheriting From?

陌路散爱 提交于 2019-12-23 10:58:38
问题 So this example from: http://en.cppreference.com/w/cpp/utility/variant/visit declares the specialized type: template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; Which is constructed as an r-value here: std::visit(overloaded { [](auto arg) { std::cout << arg << ' '; }, [](double arg) { std::cout << std::fixed << arg << ' '; }, [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; }, }, v); I'm

C++ - Use default template as base for specialization

时光怂恿深爱的人放手 提交于 2019-12-23 09:47:07
问题 I want to write a math vector template. I have a class which accepts type and size as template argument, with a lot of math operation methods. Now I want to write specializations where Vector<3> for instance has x, y, z as members which refer to data[0..3] respectively. The problem is that I don't know how to create a specialization which inherits everything from the default template without creating either a base class or writing everything twice. What's the most efficient way to do this?

C++ - Use default template as base for specialization

自古美人都是妖i 提交于 2019-12-23 09:46:52
问题 I want to write a math vector template. I have a class which accepts type and size as template argument, with a lot of math operation methods. Now I want to write specializations where Vector<3> for instance has x, y, z as members which refer to data[0..3] respectively. The problem is that I don't know how to create a specialization which inherits everything from the default template without creating either a base class or writing everything twice. What's the most efficient way to do this?

C++ template specialization/overloading

无人久伴 提交于 2019-12-23 09:32:15
问题 First of all, I'm sorry for the vague title of this question. I wasn't sure how to summarize it. The thing I want to achieve is the following, I want to be able to pass template non-type parameters of different types to the same class-template, resulting in different instantiations. Something like this: Foo<1>(); Foo<'1'>(); // different types of object I don't think this is possible, therefore I'm forced to do something like this template <typename T, T Val> struct Foo; template <int Val>

Overloading template by Return Type

若如初见. 提交于 2019-12-23 04:42:16
问题 Mooing Duck makes a comment here that "One function can't return multiple types. However, you can specialize or delegate to overloads, which works fine." I started thinking about that, and I'm trying to figure out, how is this legal code: template <typename T> T initialize(){ return T(13); } When called with: auto foo = initialize<int>(); auto bar = initialize<float>(); Doesn't that translate to 2 functions of the same name overloaded by return-type only? 回答1: It's not an overload, it's a