继承基础构造函数,基类是模板参数(MSVC)

时间:2017-08-19 23:34:15

标签: c++ templates inheritance visual-studio-2017 using

我尝试使用VS2017实现此代码,并获得错误:

template <class T>
class A {
    public :
    A() {}
};

template < template <class U> class T, class U>
class B : public T<U> {
    using T<U>::T;//Errors : # 'T': is not a member of 'A<U>' # 'T': symbol cannot be used in a using-declaration
};

int main() {
    B<A, int> test;
    return 0;
}

根据https://wandbox.org/

完美地使用Clang和GCC

我想知道为什么它不能使用Visual Studio,以及如何解决它。看起来VS并不想考虑第二个&#39; T&#39;参数作为模板。

以前是另一个问题,我可以找到最接近这件事的问题。但是找不到解决它的解决方案: Inheriting constructors from a template base class

1 个答案:

答案 0 :(得分:1)

解决方法是定义typedef别名

using base_t::base_t;

并使用它从基类继承构造函数:

<?php
//array contains value
function contains_value($my_array, $value_search){
    foreach ($my_array as $key => $value) {
        if ($value === $value_search)
            return true;
    }
    return false;
}
//array contains key
function contains_key($my_array, $key_search){
    foreach ($my_array as $key => $value) {
        if ($key === $key_search)
            return true;
    }
    return false;
}

$handle   = fopen("product_list.csv", "r");
$products = array();
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        $product  = explode(",", $line);
        $category = $product[0];
        $company  = $product[1];

        if (contains_key($products, $category)) {
            if (contains_value($products, $company)) {
                //increase the count of category by 1
                $products[$category][$company] = $products[$category][$company] + 1;
            } else {
                //append new company with count 1
                array_push($products[$category], array(
                    $company,
                    1
                ));
            }
        } else {
            //initialize new company with count 1
            $products[$category] = array(
                $company,
                1
            );
        }
    }
    fclose($handle);
}

var_dump($products);
?>

这不是一个独特的编译器错误;我朦胧地回忆起旧版gcc的类似问题。