'Is there any way to control optimization of a template function in visual studio c++ 2017

I tried using #pragma optimize("", off) to selectively disable optimizations for the a() function. This does not work. Is there any way to control optimization of a template function in visual studio c++ 2017?

Header.h

#pragma once

#include <iostream>
using namespace std;
template <class T>
class my
{
public:
    void f();
};

#pragma optimize("", off)
template<class T>
void my<T>::f()
{
    double a = 0;
    for (size_t i = 0; i < 100; i++)
    {
        a++;
    }
    cout << a;
}
#pragma optimize("", on)

Header.cpp

#include <iostream>
#include <vector>
#include "Header.h"
using namespace std;
int main()
{
    my<string> a;
    a.f();
    return 0;
    system("pause");
}


Solution 1:[1]

There is a indirect method in Developer Community. https://developercommunity.visualstudio.com/t/no-way-to-control-optimization-of-a-template-funct/104899

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 uluoo