使用`std :: vector`作为模板模板参数的默认值

时间:2015-11-24 11:14:19

标签: templates c++11 anonymous-types optional-parameters

假设foo是一个采用容器类型的模板,它本身有一个模板参数来指定其值的类型:

template <template<typename val_t> class container_t>
struct foo;

为方便起见,foostd::vector作为默认容器。麻烦的是,正式std::vector正式有两个参数,其中第二个参数有默认值。这意味着... container = std::vector不起作用。

使用C ++ 11的解决方案是定义模板别名:

template<typename val_t> using vec_dummy = std::vector<val_t>;
template <template<typename val_t> class container_t = vec_dummy>
struct foo;

我不喜欢,为了便于阅读(你必须搜索vec_dummy),而且出于审美原因(没有理由说明这种类型)。

有没有办法以某种方式匿名定义模板别名?其他解决问题的方法当然也受到热烈欢迎。

PS:实际上,foo使用内部数据类型作为提供容器的值类型,因此

template <typename container_t = std::vector<int>> struct foo;

不是一种选择。

1 个答案:

答案 0 :(得分:3)

您可以使<?php require('help.php'); if(isset($_POST['submit'])){ $title = $_POST['title']; $content = $_POST['content']; $posted = $_POST['posted']; $date = date("Y-m-d H:i:s"); $ip = $_SERVER["REMOTE_ADDR"]; $rand = rand(1,1000000); if(empty($title)){ echo "Titulli eshte bosh."; }else if(empty($content)){ echo "Permbajtja eshte bosh."; }else if(empty($_FILES['file']['name'])){ echo "Imazhi eshte bosh."; }else if($_FILES['file']['name']){ $name = htmlspecialchars($_FILES['file']['name']); $ext = end((explode(".", $name))); $ext = strtolower($ext); //if no errors... if(!$_FILES['file']['error']){ //now is the time to modify the future file name and validate the file $new_file_name = date('ymdHisu'). ".". $ext; if($_FILES['file']['size'] > (6144000)){ $valid_file = false; echo 'Oops! Your file\'s size is to large.'; } elseif($ext !== "jpg" && $ext !== "png" && $ext !== "jpeg" && $ext != "gif" && $ext !== "bmp") { $valid_file = false; echo "Your file must be in jpg, jpeg, png, gif, or bmp formats."; }else{ $valid_file = true; } //if the file has passed the test if($valid_file){ //move it to where we want it to be move_uploaded_file($_FILES['file']['tmp_name'], 'images/'.$new_file_name); $exif_read = exif_read_data("images/".$new_file_name); if(!empty($exif_read['Orientation'])){ $orientation_data = exif_read_data("images/".$new_file_name)['Orientation']; } if(isset($orientation_data) && $orientation_data !== 1){ $path = "images/". $new_file_name; $buffer = ImageCreateFromJPEG($path); $exif = exif_read_data($path); if(!empty($exif['Orientation'])){ switch($exif['Orientation']){ case 8: $buffer = imagerotate($buffer,90,0); break; case 3: $buffer = imagerotate($buffer,180,0); break; case 6: $buffer = imagerotate($buffer,-90,0); break; } $image = imagejpeg($buffer, $path, 90); } } if(empty($posted)){ $posted = 'Anonim'; } $sql = "INSERT INTO problems(title, content, date, image, posted, ip) VALUES (:title, :content, :date, :image, :posted, :ip)"; $query = $db->prepare($sql); $results = $query->execute(array( ':title' => htmlentities ($title), ':content' => htmlentities ($content), ':date' => $date, ':image' => $path, ':posted' => htmlentities ($posted), ':ip' => $ip )); echo "<div id='ok'>Lajmi u raportua me sukses. Kontrollojeni <a href='index.php'>ketu</a> .</div>"; } }else{ //set that to be the returned message echo 'Ooops! Your upload triggered the following error: '.$_FILES['file']['error']; } } } ?> 成为可变参数模板模板参数(颤抖)。

container_t

这样您可以专注于值类型,但仍然保留任何分配器模板参数等。