Repository navigation
Expand file tree
/
Copy pathto_string.hpp
More file actions
106 lines (97 loc) · 3.69 KB
/
Copy pathto_string.hpp
File metadata and controls
106 lines (97 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright (C) 2015-present The DotCpp Authors.
This file is part of .C++, a native C++ implementation of
popular .NET class library APIs developed to facilitate
code reuse between C# and C++.
http://github.com/dotcpp/dotcpp (source)
http://dotcpp.org (documentation)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <dot/declare.hpp>
#include <dot/system/string.hpp>
namespace dot
{
/// Helper class to provide to_string(value) for atomic and external
/// classes where we cannot define member function to_string().
///
/// This helper class is necessary because C++ permits partial specialization
/// of classes, but not functions. Partial specialization is necessary when
/// defining to_string for a type that is itself a template, for example
/// nullable.
template <class T>
struct to_string_impl
{
/// Convert value to string; for empty or null values, return string::empty.
///
/// The purpose of making this a deleted method here is to generate the
/// following error when to_string(...) is invoked for a user defined type,
/// while to_string_impl template is not specialized for this type.
///
/// dot::string dot::to_string_impl<T>::to_string(const T &)'
/// attempting to reference a deleted function
///
/// with
/// [
/// T = {type name here}
/// ] (compiling source file system {file name here}
///
/// To eliminate this error, add template specialization of to_string_impl
/// for the specified type:
///
/// template <>
/// class to_string_impl<{type name here}>
/// {
/// public:
/// static string to_string(const {type name here}& value)
/// {
/// ...
/// }
/// };
static string to_string(const T& value) = delete;
};
/// Convert value to string; for empty or null values, return string::empty.
///
/// This function must be defined for atomic and external classes where we
/// cannot define member function to_string() via template specialization of
/// the helper type to_string_impl.
template <class T>
string to_string(const T& value)
{
// If the compiler generates the following error at this location:
//
// dot::string dot::to_string_impl<T>::to_string(const T &)'
// attempting to reference a deleted function
//
// with
// [
// T = {type name here}
// ] (compiling source file system {file name here}
//
// the reason is most likely that to_string(...) was invoked for a
// user defined type, while to_string_impl template was not specialized
// for this type.
//
// To eliminate this error, add template specialization of to_string_impl
// for the specified type:
//
// template <>
// class to_string_impl<{type name here}>
// {
// public:
// static string to_string(const {type name here}& value)
// {
// ...
// }
// };
return to_string_impl<T>::to_string(value);
}
}