Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.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/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
C++ 在语法树中的节点上定义了层次结构的表达式模板_C++_Templates_Inheritance_Expression Templates - Fatal编程技术网

C++ 在语法树中的节点上定义了层次结构的表达式模板

C++ 在语法树中的节点上定义了层次结构的表达式模板,c++,templates,inheritance,expression-templates,C++,Templates,Inheritance,Expression Templates,我发现了对表达式模板的极好解释。在本文中,我们找到了一个基本的算术表达式模板实现,如下所示(稍作修改): 并编写一个小测试程序: int main(int argc, char const* argv[]) { double xd = 2.0, yd = 5.0; Variable x{xd}; SpecialVariable y{yd}; x += 3.0 + y; std::cout << "result : " << std:

我发现了对表达式模板的极好解释。在本文中,我们找到了一个基本的算术表达式模板实现,如下所示(稍作修改):

并编写一个小测试程序:

int main(int argc, char const* argv[]) {
    double xd = 2.0, yd = 5.0;
    Variable x{xd};
    SpecialVariable y{yd};
    x += 3.0 + y;
    std::cout << "result : " << std::to_string(x.eval()) << "\n";
    return 0;
}
这是完全合理的,因为如果模板类的模板参数有关系,则它们不一定有关系

问题:如何编写一个
运算符+=
,该运算符接受类型为且可能为其子类型的表达式模板?我还没有看到一个表达式模板教程来解决这个特殊的问题

问题:我如何编写一个
操作符+=
,它接受带有类型和可能的子类型的表达式模板

使用
std::是
和SFINAE的基础吗

template <typename V>
std::enable_if_t<std::is_base_of_v<Variable, V>>
   operator+= (BinaryExpr<double, V, plus<double>> expr)
 { _val += 1000.0; }
模板
std::如果启用,则启用
运算符+=(二进制expr expr)
{u val+=1000.0;}
前面的代码是用C++17编译的

如果您使用的是C++14,则必须使用

std::is_base_of<Variable, V>::value
typename std::enable_if<std::is_base_of<Variable, V>::value>::type
std::是::值的基础吗
而不是

std::is_base_of_v<Variable, V>
std::enable_if_t<std::is_base_of_v<Variable, V>>
std::是v的基础吗
如果您使用的是C++11,则必须使用

std::is_base_of<Variable, V>::value
typename std::enable_if<std::is_base_of<Variable, V>::value>::type
typename std::enable\u if::type
而不是

std::is_base_of_v<Variable, V>
std::enable_if_t<std::is_base_of_v<Variable, V>>
std::启用
问题:我如何编写一个
操作符+=
,它接受带有类型和可能的子类型的表达式模板

使用
std::是
和SFINAE的基础吗

template <typename V>
std::enable_if_t<std::is_base_of_v<Variable, V>>
   operator+= (BinaryExpr<double, V, plus<double>> expr)
 { _val += 1000.0; }
模板
std::如果启用,则启用
运算符+=(二进制expr expr)
{u val+=1000.0;}
前面的代码是用C++17编译的

如果您使用的是C++14,则必须使用

std::is_base_of<Variable, V>::value
typename std::enable_if<std::is_base_of<Variable, V>::value>::type
std::是::值的基础吗
而不是

std::is_base_of_v<Variable, V>
std::enable_if_t<std::is_base_of_v<Variable, V>>
std::是v的基础吗
如果您使用的是C++11,则必须使用

std::is_base_of<Variable, V>::value
typename std::enable_if<std::is_base_of<Variable, V>::value>::type
typename std::enable\u if::type
而不是

std::is_base_of_v<Variable, V>
std::enable_if_t<std::is_base_of_v<Variable, V>>
std::启用

当我使用您的C++14替代方案时,我得到以下
错误:依赖名称'std::is_base_of::type'被解析为非类型,但实例化会产生一个类型
注意:如果类型的意思是
(使用
g++-7.4.0
和上述任一标准)^请说'typename std::is_base_of::type',我犯了一个错误(
::type
vs
::value
is_base_of
之后)当我使用您的C++14替代方案时,我得到以下
错误:依赖名称'std::is_base_of::type'被解析为非类型,但是实例化会产生一个类型
注意:如果一个类型的意思是
(使用
g++-7.4.0
和上述任一标准)^没关系,我犯了一个错误(
::type
vs
::value
之后是