C ++ - 通过enable_if_t

时间:2016-03-26 05:04:02

标签: c++ templates c++14 sfinae variadic

更新:
谢谢Jamboree 这是最后的struct A

struct A
{
    template<class ... Args,class=std::enable_if_t<(sizeof...(Args)!=1)>>
    A(Args &&...args)
    {
        cout<<'v';
    }
    template<class Arg,class=std::enable_if_t<!std::is_base_of<std::remove_reference_t<Arg>,A>::value>>
    A(Arg &&arg)
    {
        cout<<'v';
    }
    A(const A &)
    {
        cout<<'c';
    }
    A(A &&)
    {
        cout<<'m';
    }
};

产地:
关于此代码,

#include<iostream>
#include<type_traits>
#include<utility>
using namespace std;

struct A
{
    template<class ... Args,class=std::enable_if_t<
        sizeof...(Args)!=1
        ||!std::is_same<std::remove_cv_t<std::remove_reference_t<Args>>,A>::value>>
    A(Args &&...args)
    {
        cout<<'v';
    }
    A(const A &)
    {
        cout<<'c';
    }
    A(A &&)
    {
        cout<<'m';
    }
};

int main()
{
    A a{10};
    A b{10,20};
    A c{b};
    const A d{c};
    A e{move(c)};
}

VC ++ 14.0中的输出为vvvvm 但为什么输出不是vvccm
(我希望c and d使用复制构造函数。我知道Effective Modern C ++ Item 27只使用一个转发引用。)

1 个答案:

答案 0 :(得分:3)

bc传递给A&c的ctor时,dArgs &&...args对编译器(不是const限定)是const A &struct A { A() = default; template<class ... Args,std::enable_if_t<(sizeof...(Args)>1), bool> = true> A(Args &&...args) { cout<<'v'; } template<class Arg,std::enable_if_t<!std::is_base_of<A, std::remove_reference_t<Arg>>::value, bool> = true> A(Arg && arg) { cout<<'v'; } A(const A &) { cout<<'c'; } A(A &&) { cout<<'m'; } }; 因此比$api_version = '78.0'; $api_endpoint = 'https://api-3t.sandbox.paypal.com/nvp'; $api_username = 'platfo_1255077030_biz_api1.gmail.com'; $api_password = '1255077037'; $api_signature = 'Abg0gYcQyxQvnf2HDJkKtA-p6pqhA1k-KTYE0Gcy1diujFio4io5Vqjf'; $creditcardtype = 'VISA'; $cardno = '4854897648835021'; $expdate = '112020'; $cvv = '176'; $PRICE = '50.00'; $currency_code = 'HKD'; $request_params = array( 'USER' => $api_username, 'PWD' => $api_password, 'SIGNATURE' => $api_signature, 'VERSION' => $api_version, 'PAYMENTACTION' => 'SALE', 'CREDITCARDTYPE' => $creditcardtype, 'ACCT' => $cardno, 'EXPDATE' => $expdate, 'CVV2' => $cvv, 'AMT' => $PRICE, 'CURRENCYCODE' => $currency_code, 'FIRSTNAME'=>'James', 'LASTNAME'=>'Smith', 'STREET'=>'FirstStreet', 'CITY'=>'SanJose', 'STATE'=>'CA', 'COUNTRYCODE'=>'US', 'ZIP'=>95131 ); $request_params['METHOD'] = 'DoDirectPayment'; $nvp_string = ''; foreach($request_params as $var=>$val) { $nvp_string .= '&'.$var.'='.urlencode($val); } $curl = curl_init(); curl_setopt($curl, CURLOPT_VERBOSE, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_URL, $api_endpoint); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $nvp_string); $result = curl_exec($curl); curl_close($curl); $response = NVPToArray($result); echo '<pre>'; print_r($response); echo '</pre>'; function NVPToArray($NVPString) { $proArray = array(); while(strlen($NVPString)) { $keypos= strpos($NVPString,'='); $keyval = substr($NVPString,0,$keypos); $valuepos = strpos($NVPString,'&') ? strpos($NVPString,'&'): strlen($NVPString); $valval = substr($NVPString,$keypos+1,$valuepos-$keypos-1); $proArray[$keyval] = urldecode($valval); $NVPString = substr($NVPString,$valuepos+1,strlen($NVPString)); } return $proArray; } 更好。

为了实现你想要的,你可以这样做:

        private void addActionPerformed(java.awt.event.ActionEvent evt) {                                    
    // TODO add your handling code here:
    try{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String d = sdf.format(tdate.getDate());
        System.out.println(d);

    String sql ="INSERT INTO TRACK(TRACKID,TRACKNAME,TRACKDESC,TRACKDATE)VALUES (?,?,?,?)";
        ps=conn.prepareStatement(sql);
        ps.setString(1,tid.getText());
        ps.setString(2,tname.getText());
        ps.setString(3,td.getText());
        ps.setString(4,d);
        ps.execute();

        JOptionPane.showMessageDialog(null, "Added");
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null,e);
    }

}