Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;常量类型的模板泛化_C++_Templates_Constants_Template Specialization - Fatal编程技术网

C++ C++;常量类型的模板泛化

C++ C++;常量类型的模板泛化,c++,templates,constants,template-specialization,C++,Templates,Constants,Template Specialization,我在这里做一个小的研究,这需要在某个阶段,我有不同的类对一些数据进行(或不进行)操作,这取决于它的常量 一个小例子是这样的() 我发现以下编译错误: main.cpp: In instantiation of 'funny<T>::funny(T&) [with T = const char&]': main.cpp:21:7: required from here main.cpp:7:28: error: assignment of read-only loc

我在这里做一个小的研究,这需要在某个阶段,我有不同的类对一些数据进行(或不进行)操作,这取决于它的常量

一个小例子是这样的()

我发现以下编译错误:

main.cpp: In instantiation of 'funny<T>::funny(T&) [with T = const char&]':
main.cpp:21:7:   required from here
main.cpp:7:28: error: assignment of read-only location '((funny<const char&>*)this)->funny<const char&>::v'
  funny(T& a) : v(a) {v -= 1; }
                      ~~^~~~
main.cpp: In instantiation of 'funny<T>::~funny() [with T = const char&]':
main.cpp:21:7:   required from here
main.cpp:8:27: error: assignment of read-only location '((funny<const char&>*)this)->funny<const char&>::v'
  virtual ~funny() { v += 1; }
                     ~~^~~~
main.cpp:在'funcy::funcy(T&)[with T=const char&]的实例化中:
main.cpp:21:7:从这里开始需要
main.cpp:7:28:错误:指定只读位置“((有趣的*)this)->有趣的::v”
有趣的(T&a):v(a){v-=1;}
~~^~~~
main.cpp:在“funcy::~funcy()[with T=const char&]”的实例化中:
main.cpp:21:7:从这里开始需要
main.cpp:8:27:错误:分配只读位置“((有趣的*)this)->有趣的::v”
virtual~funcy(){v+=1;}
~~^~~~
这是完全可以理解的,因为我试图修改一个常数。编译器就在这里。但是,我确实需要它来处理常量数据,因此我尝试创建模板的常量专门化:

template <class T>
class funny <T const>
{
public:
    funny(const T& a) : v(a) {}
    operator T() {return v;}

private:
    const T& v;
};
模板
课堂搞笑
{
公众:
有趣的(const T&a):v(a){}
运算符T(){return v;}
私人:
康斯特电视台;
};
但无论如何,编译器找不到它,仍然尝试编译非常量版本


关于如何做到这一点,有什么想法吗?

如果您更改以下内容,将编译:

template <class T>
class funny <T const>
模板
课堂搞笑
致:

模板
课堂搞笑
decltype(t[0])
推断为
常量字符&
,这与您的
常量字符
专业化不匹配。您有两个选择:

1) 将专门化更改为
模板类
。这将适用于这种情况,但不适用于
const int FOO=42;五(富);

2) 将
V
宏更改为始终推断为非参考类型:

#define V(a) funny<typename std::remove_reference<decltype(a)>::type>(a)
#定义V(a)有趣(a)

正如您在错误消息中所看到的,
T
被推断为
const char&
,这是一种引用类型,因此不是const限定的(
T
X&
其中
X
const char
)。这是因为
t[0]
是一个表达式,作为一个表达式,它是一个左值<这样一个表达式的code>decltype(e)
会产生一个左值引用类型。确实如此:)谢谢!请注意,这是因为
decltype(t[0])
是一个引用。这对于一些
const char
来说是行不通的@迈尔斯的回答更有力。@TartanLlama我同意
template <class T>
class funny <T const>
template <class T>
class funny <const T&>
#define V(a) funny<typename std::remove_reference<decltype(a)>::type>(a)