У ученика есть std::array с именем name и целочисленный возраст. У учащегося есть функция-член под названием encode, которая вызывает глобальную функцию шаблона encode, используя name.size() в качестве параметра шаблона.
код показан ниже:
Код: Выделить всё
//main.cpp
#include
#include
template
unsigned long encode(unsigned long num2){
return num1 + num2;
}
struct student {
std::array name;
int age;
student(const std::array& name, int age):
name(name),
age(age)
{}
unsigned long encode(){
return ::encode(age);
}
};
int main(){
std::array name = {"Tim"};
student Tim(name, 17);
std::cout g++ main.cpp -std=c++11
main.cpp: In member function 'long unsigned int student::encode()':
main.cpp:22:43: error: use of 'this' in a constant expression
22 | return ::encode(age);
| ^
main.cpp:22:45: error: no matching function for call to 'encodestudent::name.std::array::size()>(int&)'
22 | return ::encode(age);
| ~~~~~~~~~~~~~~~~~~~~~^~~~~
main.cpp:9:15: note: candidate: 'template long unsigned int encode(long unsigned int)'
9 | unsigned long encode(unsigned long num2){
| ^~~~~~
main.cpp:9:15: note: template argument deduction/substitution failed:
main.cpp:22:45: error: use of 'this' in a constant expression
22 | return ::encode(age);
| ~~~~~~~~~~~~~~~~~~~~~^~~~~
main.cpp:22:42: note: in template argument for type 'long unsigned int'
22 | return ::encode(age);
Код: Выделить всё
g++ version: 14.1.0
Подробнее здесь: https://stackoverflow.com/questions/793 ... a-non-cons